[英]Get word between two numbers java regex
I want to get the name word between the first id and before the second number.我想在第一个 id 和第二个数字之前获取名称单词。 I want to do this in Java Regex.
我想在 Java 正则表达式中做到这一点。
eg Car Care or Car Electronics & Accessories例如汽车保养或汽车电子及配件
# Name Id Child nodes
1 Car Care 15718271 Browse
2 Car Electronics & Accessories 2230642011 Browse
3 Exterior Accessories 15857511 Browse
I tried splitting the line with .split(" ")[1] but then it splits the words with spaces.我尝试用 .split(" ")[1] 分割行,但随后它用空格分割单词。 Only gives one word within a phrase eg Car
在一个短语中只给出一个词,例如 Car
Try this one: ^\d*[a-zA-Z &+-]*(\d*)[a-zA-Z ]*
In the match '(\d*)' you will find the wished number.试试这个:
^\d*[a-zA-Z &+-]*(\d*)[a-zA-Z ]*
在匹配 '(\d*)' 中你会找到想要的数字。 If the strings before and after the number have special characters add them to the appropiate [] sections.如果数字前后的字符串有特殊字符,请将它们添加到适当的 [] 部分。 Explaination: '^' says start from the beginning, '\d*' take the first digit one or multiple times, [a-zA-Z &+-] take a string with these characters, (\d*) specifies the wished number, [a-zA-Z ] again takes a string after the number.
说明:'^' 表示从头开始,'\d*' 取第一个数字一次或多次,[a-zA-Z &+-] 取这些字符的字符串,(\d*) 表示希望number, [a-zA-Z ] 再次在数字后面加上一个字符串。 Use a regex editor for trying this out.
使用正则表达式编辑器进行尝试。
You can use您可以使用
(?m)^\d+\s+(.*?)\s+\d{6,}\s
See the regex demo .请参阅正则表达式演示。 Details :
详情:
(?m)
- a multiline option (?m)
- 多行选项^
- start of a line ^
- 行首\d+
- one or more digits \d+
- 一位或多位数字\s+
- one or more whitespaces \s+
- 一个或多个空格(.*?)
- Group 1: zero or more chars other than line break chars as few as possible (.*?)
- 第 1 组:除换行符之外的零个或多个字符尽可能少\s+
- one or more whitespaces \s+
- 一个或多个空格\d{6,}
- six or more digits \d{6,}
- 六位或更多位\s
- a whitespace. \s
- 一个空格。 See the Java demo :请参阅Java 演示:
String s = "# Name Id Child nodes\n1 Car Care 15718271 Browse \n2 Car Electronics & Accessories 2230642011 Browse\n3 Exterior Accessories 15857511 Browse";
Pattern pattern = Pattern.compile("(?m)^\\d+\\s+(.*?)\\s+\\d{6,}\\s");
Matcher matcher = pattern.matcher(s);
while (matcher.find()){
System.out.println(matcher.group(1));
}
Output:输出:
Car Care
Car Electronics & Accessories
Exterior Accessories
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.