why this java regex is not working as I want?
These are two type of string I can get in input:
String mex1 = "/ABCD/YY YYYYY SPA QWE XXXXX XXXXX SPA - 开发者_运维百科02342990153*XXXXX XXXX SPA ABCD LEGALI";
String mex2 = "/ABCD/YY YYYYY SPA QWE XXXXX XXXXX SPA - 02342990153*XXXXX XXXX SPA ABCD LEGALI/ORDERS/9865432342990160";
Which fall in two possible cases, with */some_word/some_number* and without it
I have written this regex which is giving a result I don't understand:
String mex=//<one of two input cases as already explained>
Pattern p = Pattern.compile("(/ABCD/)(.+ )(/\\w+/\\d+)?");
Matcher m = p.matcher(mex);
if(m.find()) {
System.out.println(m.group(1));
System.out.println(m.group(2)); // this the group I would like to retrieve...
}
And the result is:
mex2
/ABCD/
YY YYYYY SPA QWE XXXXX XXXXX SPA - 02342990153*XXXXX XXXX SPA ABCD LEGALI
mex1
/ABCD/
YY YYYYY SPA QWE XXXXX XXXXX SPA - 02342990153*XXXXX XXXX SPA ABCD
which is not what I expected in particular with mex2, where the string I want to retrieve get truncated. Also, why after including the boundaries results in match find = false ?
Pattern p = Pattern.compile("^(/ABCD/)(.+ )(/\\w+/\\d+)?$");
thanks
There are two issues with your regex:
- The
(.+ )
requires a space at the end of the group. - The
.+
is a greedy match so it matches the remainder of the string.
Try the following regex:
/ABCD/(.+?)(/\\w+/\\d+)?$
Try this one
(/ABCD/)([^\/]+(/\\w+/\\d+)?)
The [^\/]
will capture anything but /
精彩评论