Home > other >  Regex if/else Statement?
Regex if/else Statement?

Time:12-22

My current pattern looks like this: (line:\s*) ([A-z,0-9;.] )

which normally works great, to match the word after every 'line:'.

enter image description here

But I've found some special cases in my test string, where it doesn't work right.

enter image description here

It doesn't match the strings starting with '--', which is also right, but instead of matching nothing, it should match the next String who matches no matter how many strings before start with '--'.

Is there a way to add an if/else Statement like: if string starts with '--' then match next string.

Or is there any other solution?

CodePudding user response:

If you want to ignore white spaces AND substrings start with --, try

(line:(?:\s|--.*)*)([A-z,0-9;.] )

The only difference is (?:\s|--.*)*.

Not only it captures white spaces \s, it also captures comments --.*.

See the test cases

  • Related