简体   繁体   English

在Java中使用正则表达式从字符串中提取信息

[英]Using Regular Expression in Java to extract information from a String

I have one input String like this: 我有一个这样的输入String

"I am Duc/N Ta/N Van/N" 

String "/N" present it is the Name of one person. 字符串"/N"表示它是一个人的名字。
The expected output is: 预期的输出是:

Name: Duc Ta Van 名称:德塔范

How can I do it by using regular expression? 我该如何使用正则表达式呢?

Here is the regex to use to capture every "name" preceded by a /N 这是用于捕获每个以“ /N ”开头的“名称”的正则表达式

(\w+)\/N

Validate with Regex101 使用Regex101验证

Now, you just need to loop on every match in that String and concatenate the to get the result : 现在,您只需要在该String每个匹配项上循环并连接即可获得结果:

String pattern = "(\\w+)\\/N";
String test = "I am Duc/N Ta/N Van/N";
Matcher m = Pattern.compile(pattern).matcher(test);

StringBuilder sbNames = new StringBuilder(); 
while(m.find()){
    sbNames.append(m.group(1)).append(" ");
}
System.out.println(sbNames.toString());

Duc Ta Van 德塔范

It is giving you the hardest part. 它给了您最难的部分。 I let you adapt this to match your need. 我让您根据自己的需要进行调整。

Note : 注意 :
In java, it is not required to escape a forward slash, but to use the same regex in the entire answer, I will keep "(\\\\w+)\\\\/N" , but "(\\\\w+)/N" will work as well. 在Java中,不需要转义正斜杠,但是要在整个答案中使用相同的正则表达式,我将保留"(\\\\w+)\\\\/N" ,但是"(\\\\w+)/N"将也可以

You can use Pattern and Matcher like this : 您可以像这样使用Pattern和Matcher:

String input = "I am Duc/N Ta/N Van/N";
Pattern pattern = Pattern.compile("([^\\s]+)/N");
Matcher matcher = pattern.matcher(input);
String result = "";
while (matcher.find()) {
    result+= matcher.group(1) + " ";
}

System.out.println("Name: " + result.trim());

Output 输出量

Name: Duc Ta Van

Another Solution using Java 9+ 使用Java 9+的另一种解决方案

From Java9+ you can use Matcher::results like this : 在Java9 +中,您可以使用Matcher::results这样的Matcher::results

String input = "I am Duc/N Ta/N Van/N";
String regex = "([^\\s]+)/N";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(input);
String result = matcher.results().map(s -> s.group(1)).collect(Collectors.joining(" "));

System.out.println("Name: " + result); // Name: Duc Ta Van

I've used "[/N]+" as the regular expression. 我已经使用“ [/ N] +”作为正则表达式。

Regex101 正则表达式101

[] = Matches characters inside the set [] =匹配集合中的字符

\\/ = Matches the character / literally (case sensitive) \\/ =匹配字符/按字面值(区分大小写)

+ = Matches between one and unlimited times, as many times as possible, giving back as needed (greedy) + =匹配一次到无限次,并尽可能多地匹配,并根据需要返回(贪婪)

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

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