java - Will this regex always work according to requirements stated below? -
is regex correct break sentence 3 tokens:
- characters before lowercased letters inside parentheses
- lowercase letters inside parentheses including parentheses
- characters after lowercased parentheses letters
system.out.println("this (a) test".matches("^(.*)?\\([a-z]*\\)(.*)?$"));
the string may or may not have parentheses lower cased letter , may appear anywhere in sentence. if see flaw in use case haven't considered, can provide correction in regex ?
for e.g. above.
group1 captures group2 captures (a) group3 captures test
edit:: how change regex achieve following ?
if string has (foo)(bar)(baz) how capture group1= empty group2=(foo) , group3=empty. , find above pattern thrice because there 3 parentheses.
separate examining regex, whenever write regex, write series of unit tests cover each case. i'd suggest same. create 4 tests (at least) using regex , testing against strings:
- (a) test
- this (a) test
- this test (a)
- this test
that should cover each of cases you've described. that's easier , faster trying hand analyze regex each case.
Comments
Post a Comment