简体   繁体   English

Java正则表达式用于第一个匹配的字符串

[英]Java Regex for first matching String

I have a string hhht . 我有一个字符串hhht I need to grep 12121212 & 56565656 from the string. 我需要从字符串grep 1212121256565656 What i tried so far is shown below. 我到目前为止尝试过的内容如下所示。

String hhht = "dhdhdh<a:Rakesh>12121212</a:Rakesh>sdsdvsdvsvvsv"+"sfsf"+"<a:Rakesh>56565656</a:Rakesh>zvnbjvbj";

Pattern pattern    = Pattern.compile("<a:Rakesh>(.+)</a:Rakesh>");
Matcher matcher    = pattern.matcher(hhht);

for(int hh = 0 ;hh <matcher.groupCount(); hh++){
    if(matcher.find())
        System.out.println(matcher.group(hh+1));

}

I got the output as, 我得到的输出是,

12121212</a:Rakesh>sdsdvsdvsvvsvsfsf<a:Rakesh>56565656

ie, the pattern is matching to the first <a:Rakesh> tag. 即,该模式与第一个<a:Rakesh>标签匹配。

  1. Use non-greedy regex with DOTALL flag: 使用带有DOTALL标志的非贪婪正则表达式:

     Pattern pattern = Pattern.compile("(?s)<a:Rakesh>(.+?)</a:Rakesh>"); 
  2. And you cannot get matcher.groupCount() before calling find 而且您无法在调用find之前获取matcher.groupCount()

Use it like this 这样使用

if(matcher.find()) {
   for(int hh = 0; hh <= matcher.groupCount(); hh++){
        System.out.println(matcher.group(hh+1));    
   }
}

You have a greedy matcher which is not limited to matching numbers. 您有一个贪婪的匹配器,不仅限于匹配号。 This means that it will match as much as possible . 这意味着它将尽可能匹配。 Since you have two matching tags it grabs every character between the opening of the first tag and the closing of the second tag. 由于您有两个匹配的标签,因此它将捕获第一个标签的开头和第二个标签的结尾之间的每个字符。

You can make it non greedy (it will then stop as early as possible, stopping at the first </a:Rakesh> ) or make it only match numbers (which will not match </a:Rakesh> , stopping at that point). 您可以使其不贪心(它将尽早停止,在第一个</a:Rakesh>处停止)或使其仅与数字匹配(与</a:Rakesh>不匹配,然后在该点停止) 。

This matches only numbers: 仅匹配数字:

"<a:Rakesh>(\\d+)</a:Rakesh>"

This is the non greedy approach: 这是非贪婪的方法:

"<a:Rakesh>(.+?)</a:Rakesh>"

This depends on greeding matching: Take this pattern: 这取决于问候匹配:采用以下模式:

Pattern pattern    = Pattern.compile("<a:Rakesh>(.+?)</a:Rakesh>");

For more information look this thread . 有关更多信息, 请参见此线程

And you should use a while loop: 您应该使用while循环:

    while (matcher.find()) {
            System.out.println(matcher.group(1));

    }       

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

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