java - separate the two matches -


Rex problem: I want to get the group twice, but I do not know how to solve it.

Here is the code:

  public static zero multiGroupTest () {// pattern p = pattern.compile ("(\\ w +) (\\ d \\ D) (\\ w +) "); Pattern p = Pattern.compile ("([A-Z] {1}) (\\ d +) ([A-Za-z] +)"); String str = "X89SuperJavaJavaX89SuperJava"; Mitcher M = P. Matcher (str); While (m.find ()) {System.out.println (m.group (1)); Println (m.group (2)); Println (m.group (3)); Okay, the result is:  
  x 89 super java java x  

What do I get to:

  X 89 Super Jawazawa X 89 Super Java How can I separate two matches?   

because the last group ([A-Za-z] +) greed Will match the following X, you could not get two wire. Use ((?: [AZ] [az] +) + / to capture the words contained in the FooBar format, because the name does not end with a capital letter Will be

  ([AZ]) (\\ d +) ((?: [AZ] [az] +) +)  

  pattern p = pattern.compile ("([AZ]) (\\ d +) ((?: [AZ] [az] +) +); String str = "X89SuperJavaJavaX89SuperJava"; Mitcher M = P. Matcher (str); While (m.find ()) {System.out.println (m.group (1)); Println (m.group (2)); Println (m.group (3)); }  

Output:

  X 89 super java java x 89 super java  

Comments

Popular posts from this blog

apache - 504 Gateway Time-out The server didn't respond in time. How to fix it? -

c# - .net WebSocket: CloseOutputAsync vs CloseAsync -

c++ - How to properly scale qgroupbox title with stylesheet for high resolution display? -