#9663
CrashNBurn
Member

The (? syntax is something entirely different.

Example, given the following Text

This is a test sentence
This is a test sentence
of a regex string
of a regex string

Try this regex:
Search: ^(.*)(?|(a test )|(a regex ))(.*)$
Replace: 3

With the (?| syntax, there will only be 3 pieces, since the section inside (?|…) isn’t counted as a bracket set.
For the first line:
1: This is_
2: a test_
3: sentence

The lines will be replaced with “sentence” and “string”.

Now without the (?| syntax, eg:
Search: ^(.*)((a test )|(a regex ))(.*)$ that regex would contain 5 pieces that would be matched with 1 thru 5, and some of them would be empty, depending on the line.

For the first line:
1: This is_
2: a test_
3: a test_
4:
5: sentence

The (? syntax becomes important if you utilize more complex regexes due to the nature of regex to match everything it absolutely can when things like “.*” are present.

Example 2A:
Search: ^(.*)( is a | a regex )?
Replace: 1_

That regex will match the whole line, and the replace wont change anything — as the .* consumes the whole line, since the part in (brackets)? is optional, the greedy regex wont see it.

Example 2B:
Search: ^(.*)(?|( is a )|( a regex ))
Replace: 1_

This regex will actually remove ” is a ” and ” a regex ” from the lines in question.

A lot of editors don’t even implement the full regex spec, e.g. Notepad2 does a small subset which doesn’t even handle the optional “?” correctly, and certainly doesn’t support the (?| syntax.