Java 如何排除“<&引用;在正则表达式中匹配
我有一个字符串,看起来像Java 如何排除“<&引用;在正则表达式中匹配,java,regex,Java,Regex,我有一个字符串,看起来像和“。我必须得到这样的结果 1) <name> 2) <address> 3) <Phone_1> 1) 2) 3) 我尝试使用regex“”,但它只返回一个结果。您想要的regex是 <([^<>]+?)><([^<>]+?)> and <([^<>]+?)> 和 然后在3个捕获组中吐出你想要的东西。完整的代码如下所示: Matcher m = Pat
和“
。我必须得到这样的结果
1) <name>
2) <address>
3) <Phone_1>
1)
2)
3)
我尝试使用regex“”,但它只返回一个结果。您想要的regex是
<([^<>]+?)><([^<>]+?)> and <([^<>]+?)>
和
然后在3个捕获组中吐出你想要的东西。完整的代码如下所示:
Matcher m = Pattern.compile("<([^<>]+?)><([^<>]+?)> and <([^<>]+?)>").matcher(string);
if (m.find()) {
String name = m.group(1);
String address = m.group(2);
String phone = m.group(3);
}
Matcher m=Pattern.compile(“and”).Matcher(字符串);
if(m.find()){
字符串名称=m.group(1);
字符串地址=m.group(2);
字符串电话=m组(3);
}
您想要的正则表达式是
<([^<>]+?)><([^<>]+?)> and <([^<>]+?)>
和
然后在3个捕获组中吐出你想要的东西。完整的代码如下所示:
Matcher m = Pattern.compile("<([^<>]+?)><([^<>]+?)> and <([^<>]+?)>").matcher(string);
if (m.find()) {
String name = m.group(1);
String address = m.group(2);
String phone = m.group(3);
}
Matcher m=Pattern.compile(“and”).Matcher(字符串);
if(m.find()){
字符串名称=m.group(1);
字符串地址=m.group(2);
字符串电话=m组(3);
}
正则表达式中的模式*
是贪婪的。它将在找到的第一个
和最后一个可能的代码>之间匹配尽可能多的字符。对于您的字符串,它会找到第一个
,它会在字符串的最末端找到
您需要一个非贪婪的或“懒惰”模式,它将匹配尽可能少的字符。只需
。问号是非贪婪的语法。另请参见。正则表达式中的模式*
是贪婪的。它将在找到的第一个
和最后一个可能的代码>之间匹配尽可能多的字符。对于您的字符串,它会找到第一个
,它会在字符串的最末端找到
您需要一个非贪婪的或“懒惰”模式,它将匹配尽可能少的字符。只需
。问号是非贪婪的语法。另请参见。如果您有动态的组数,则此操作将有效
Pattern p = Pattern.compile("(<\\w+>)");
Matcher m = p.matcher("<name><address> and <Phone_1>");
while (m.find()) {
System.out.println(m.group());
}
Pattern p=Pattern.compile(“()”);
匹配器m=p.Matcher(“和”);
while(m.find()){
System.out.println(m.group());
}
如果您有动态的组数,这将起作用
Pattern p = Pattern.compile("(<\\w+>)");
Matcher m = p.matcher("<name><address> and <Phone_1>");
while (m.find()) {
System.out.println(m.group());
}
Pattern p=Pattern.compile(“()”);
匹配器m=p.Matcher(“和”);
while(m.find()){
System.out.println(m.group());
}
什么语言…?嗨@Andy。。这是用什么语言写的…?嗨,安迪。。它是用Java编写的