Confusion of regex capture groups

I have the following text

July 31, 2015 - Departure 2 stops Total en route: 20 h 40 m
August 26, 2015 - Return 1 stop Total travel time: 19 h 0 m Chicago
nonstop

I want to get the digit that precedes the text that looks like "stop (s)" and all instances of "nonstop", however I want both to be in the same capture group.

I wrote this regex (\d)(?:\Wstops?)|(nonstop)

This does what I want, but as you can see it has two capture groups (group # 1 for numbers and group # 2 for "nonstop"), can this be done with one capture group?

My other question is, is it possible to directly return 'nonstop' as 0 using regex, instead of having to process the text later in the code?

Here is a live demo of my regex: regex101

+3


source to share


1 answer


You need to use a positive lookahead expression .

Matcher m = Pattern.compile("\\d(?=\Wstops?)|nonstop").matcher(s);
while(m.find())
{
System.out.println(m.group());
}

      



  • \\d(?=\Wstops?)

    matches all digits only if followed by a non-word character followed by a string, stop

    orstops

  • |

    OR

  • nonstop

    matches string nonstop

+4


source







All Articles