Search code examples
regexregex-lookaroundsregex-group

how to find only words after equal character with regex?


For example, in this text:

source=sourses/date=2018-02-05
date=2018-02-05/source=swbsource
flight_source=soursewft/date=2018-02-05
date=2018-02-05/source=sources
source=sourseswqa/date=2018-02-05
date=2018-02-05/flight_source=sourcepdt

I want to match the only words after equal. I mean the only sourseswqa, swbsource, sources, sourcepdt, sourses, soursewft words. but it could not be only 'source' or 'flight_source'


Solution

  • Let's break it down: According to your description, you are interested in the first word following an equals character when the equals character follows the word "source" or "flight_source". The captured word is then terminated by a whitespace character, a forward slash or the end of the string.

    A regex similar to this should do the trick:

    (?:flight_source|source)=([^\/\s]+)

    The first group is a non capturing group that matches either "source" or "flight_source". We then look for an equals sign. After finding an equals sign we start a capturing group where we capture one or more characters that are NOT a forward slash or a whitespace character.