Java support for conditional lookahead

≯℡__Kan透↙ 提交于 2019-12-10 16:24:49

问题


In the following let's say zip codes I am trying to exclude the 33333- from the result.
I do:

String zip = "11111 22222 33333- 44444-4444";
String regex = "\\d{5}(?(?=-)-\\d{4})";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(zip);
while (matcher.find()) { 
   System.out.println(" Found: " + matcher.group());     
}

Expect to get:

Found:  11111  
Found:  22222  
Found:  44444-4444

I am trying to enforce format of:
5 digits optionally followed by a - and 4 digits. 5 digits with just a - (hyphen) is not wanted

I get exception:

Exception in thread "main" java.util.regex.PatternSyntaxException: Unknown inline modifier near index 7
\d{5}(?(?=-)(-\d{4}))
       ^
    at java.util.regex.Pattern.error(Unknown Source)
    at java.util.regex.Pattern.group0(Unknown Source)
    at java.util.regex.Pattern.sequence(Unknown Source)
    at java.util.regex.Pattern.expr(Unknown Source)
    at java.util.regex.Pattern.compile(Unknown Source)
    at java.util.regex.Pattern.<init>(Unknown Source)
    at java.util.regex.Pattern.compile(Unknown Source)

Am I not using the conditional lookahead correctly?


回答1:


To capture all numbers except 33333 use this code:

String zip = "11111 22222 33333- 44444-4444";
String regex = "\\d{5}(?=(-\\d{4}|\\s|$))(-\\d{4})?";
Matcher m = Pattern.compile(regex).matcher(zip);
while(m.find())
    System.out.printf("Macthed: [%s]%n", m.group(1));

OUTPUT:

Macthed: [11111]
Macthed: [22222]
Macthed: [44444-4444]

Explanation: This RegEx is using lookahead that itself is like a condition, which means match 5 digit number which must be followed by - and 4 digits OR a space OR end of string and then it is optionally matching a text - and 4 digits.

The reason why your original RegEx is throwing exception because there is a syntax error in ?:(?=-) part of your RegEx.




回答2:


You'r missing a colon after (?, i.e. use this regex (non-Java-String): \d{5}(?:(?=-)-\d{4}).

However, this might still not produce the result you want. Please post some example input and required output.




回答3:


Your question is a little unclear to me. I suppose you are looking for:

String st = "11111 22222 33333- 44444-4444";
String pattern = "\\d+(- )";
String res  = st.replaceAll(pattern,"");
System.out.println(res);

Output = 11111 22222 44444-4444




回答4:


(\d{5}(?!-\s)(?:-\d{4})?)

hence:

String regex = "(\\d{5}(?!-\\s)(?:-\\d{4})?)";`


来源:https://stackoverflow.com/questions/8945398/java-support-for-conditional-lookahead

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!