java - Regex capture groups confusion -


i have following text

jul 31, 2015 - departure 2 stops total travel time:20 h 40 m
aug 26, 2015 - return 1 stop total travel time:19 h 0 m chicago
nonstop

i want digit precedes text looks "stop(s)" , instances of "nonstop", want both in same capture group.

i wrote regex (\d)(?:\wstops?)|(nonstop)

this want see consists of 2 capture groups (group #1 digits , group #2 'nonstop'), can done single capture group?

my other question, possible directly return 'nonstop' 0 using regex, instead of processing text in code later on?

here live demo of regex: regex101

you need use positive lookahead assertion.

matcher m = pattern.compile("\\d(?=\wstops?)|nonstop").matcher(s); while(m.find()) { system.out.println(m.group()); } 
  • \\d(?=\wstops?) matches digits if it's followed non-word character again followed string stop or stops

  • | or

  • nonstop match string nonstop


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

jquery - javascript onscroll fade same class but with different div -