简体   繁体   English

(Java)如何在String.replaceAll()的“ replacement”参数中模拟正则表达式?

[英](Java) How to mimic a regex in the “replacement” argument of String.replaceAll()?

Don't worry, I'm not asking you to help me find a regex! 不用担心,我不是要您帮助我找到正则表达式!

I'm currently using the method 我目前正在使用该方法

String.replaceAll(String regex, String replacement)

to parse a line of user-provided data and grab the username. 解析一行用户提供的数据并获取用户名。 I'm doing this by stripping the line of everything but the data I need, then returning that data. 我这样做是通过剥离除所需数据以外的所有内容,然后返回该数据。

Let's pretend 假装

String rawTextInput = "Machine# 000111 
                       Password: omg333444"

I want the username to be the Machine number. 我希望用户名是计算机号。 My method: 我的方法:

private String getUsername(String rawTextInput) {
    String username = rawTextInput.replaceAll("(.+#\\s\\d{6,6})", "(\\d{6,6})");
    return username;
}

The first argument, regex (.+#\\s\\d{6,6}) correctly identifies the first line of the raw input, Machine# 000111 . 第一个参数regex (。+#\\ s \\ d {6,6})正确标识原始输入的第一行Machine#000111

The second argument/regular expression (\\d{6,6}) correctly isolates the data I want, 000111 . 第二个参数/正则表达式(\\ d {6,6})正确隔离了我想要的数据000111

Basically, what this method is telling the program to do is "Find the line with Machine# 000111 and strip that line to 000111 . 基本上,此方法告诉程序执行的操作是“找到带有Machine#000111的行,并将该行剥离为000111

Actual output 实际产量

However, I am obviously using the second argument of ReplaceAll() incorrectly, because I'm getting back the literal regular expression 但是,我显然不正确地使用了ReplaceAll()的第二个参数,因为我要返回原义正则表达式

username = "(\\d{6,6})"

Expected output 预期产量

instead of 代替

username = "omg333444"

What is the correct way to grab the username here? 在这里获取用户名的正确方法是什么? Is there a way to mimic the presence of a regex in the Replacement argument of ReplaceAll()? 有没有办法模仿ReplaceAll()的Replacement参数中正则表达式的存在?

Please note, the code blocks in this example have duplicate "\\" characters because they are escape characters in IntelliJ IDEA. 请注意,此示例中的代码块具有重复的“ \\”字符,因为它们是IntelliJ IDEA中的转义字符。

IntellIJ actually does this for the replace option. 实际上,IntelliJ会为replace选项执行此操作。 You can test it out by hitting Ctrl+R (or the equivalent on your platform) and playing with it. 您可以通过按Ctrl + R(或您平台上的同等功能)并对其进行测试来进行测试。

The answer is that groups are given a number based on their position while parsing. 答案是,在解析时会根据组的位置为组提供一个数字。 For example, your current Regex would be considered one group, because there is one set of parenthesis surrounding the entire match. 例如,您的当前正则表达式将被视为一个组,因为整个比赛过程中都有一组括号。 If you isolate the numbers in its own separate group, you will be able to use that in the replacement. 如果您将数字分为一个单独的组,则可以在替换组中使用。 Your new regex might look something like this: 您的新正则表达式可能如下所示:

.+#\\s(\\d{6,6})

In that, the numbers are isolated in the first group. 这样,数字在第一组中被隔离。

Groups are then denoted by their location and a $ in front of it. 然后,通过组的位置和前面的$来表示组。 So your new replace would look something like this: 因此,您的新替换项将如下所示:

String username = rawTextInput.replaceAll(".+#\\s(\\d{6,6})", "$1");

More information about the replacement character is on this thread . 有关替换字符的更多信息,请参见此线程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM