java - Will this regex always work according to requirements stated below? -


is regex correct break sentence 3 tokens:

  1. characters before lowercased letters inside parentheses
  2. lowercase letters inside parentheses including parentheses
  3. 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

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -