Refining a regex repeating group -


i'm trying extract 2 sides of string delimited hyphen

abc - def 

at moment have

([^-]*)-([^-]*) 

match 1 abc , match 2 def.

is there more elegant way of writing regular expression there no repeating elements? i.e. ([^-]*) not repeated twice.

simply use [^-]+ , iterate on results.

illustration in java:

// yours matcher m1 = pattern.compile("([^-]*)-([^-]*)").matcher("abc - def"); if (m1.find()) {     system.out.println(m1.group(1));     system.out.println(m1.group(2)); }  // mine matcher m2 = pattern.compile("[^-]+").matcher("abc - def"); while (m2.find()) {     system.out.println(m2.group()); } 

outputs identical.


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -