[Solved] Regex – Match a sentence but ignoring certian words if present [closed]


You can achieve that by either using an alternation or an optional group:

With alternation (https://regex101.com/r/qIjNWF/1):

400 people drive dark blue cars|400 people drive cars

The pipe symbol is working like a logical OR resp. alternation. So, it matches either the left side or the right side.

With an optional group (https://regex101.com/r/p5Z7eU/1/):

400 people drive (dark blue )?cars

The braces are grouping an expression and the question mark is marking this group as optional. A group is also being captured to be referenced again. If you don’t need that you can also use non-capturing groups:

400 people drive (?:dark blue )?cars

1

solved Regex – Match a sentence but ignoring certian words if present [closed]