简体   繁体   中英

Java regex matcher pattern keep returning false

I was having some problem with regex in Java. I have this regex pattern:

static Pattern parts = Pattern.compile("\\A([91|10|17|21|30].{1,20}\\s){1,5}\\Z");

Then I try to test it with dummy data:

String s = "91448629517150623101408002301";
Matcher testMatcher = parts.matcher(s);
System.out.println(testMatcher.matches());

String s1 = "9143676601715Sep14101310147301";
Matcher testMatcher1 = parts.matcher(s1);
System.out.println(testMatcher1.matches());

The dummy data is in the correct format. However, I not sure why both keep returns me false. Any ideas?

The regular expression you provided might be incorrect for matching the pattern. I think you might want to use \\s ( A whitespace character ) instead of \\S ( A non-whitespace character ).

I suggest you go through the documentation again carefully.

For example,

static Pattern parts = Pattern.compile("\\A([91|10|17|21|30].{1,20}\\S){1,5}\\Z");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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