1

Let's say I have a regular expression that looks like (using Perl syntax for a replacement, but question is meant to be generic):

s/(example|something|ex)/xxx/g

Can I assume the search is performed "in order", ie, that it will match "example" before it matches "ex"? Or applying it to a string that looks like "example1 example2" can have different results depending on the language/implementation?

(tested it on Perl and it looks like it honors the order in the regex, but is this generic?)

jvilhena
  • 1,131
  • 8
  • 21
  • 2
    It should be in order as far as I'm aware. Alternation should match the first item every time. Otherwise `ab|a` will behave very unpredictably and will never be useful. – VLAZ Apr 28 '20 at 17:40
  • Well, I found this https://www.regular-expressions.info/alternation.html that seems to imply that the engine is relevant to the way the regex is processed. "When a text-directed engine attempts Get|GetValue|Set|SetValue on SetValue, it tries all permutations of the regex at the start of the string. It does so efficiently, without any backtracking. ... It always returns the longest match, in this case SetValue." . so it looks like the answer is "it depends on the engine" – jvilhena Apr 28 '20 at 17:59
  • well, and the linked (duped) question has nothing to do with this. it's basically an incorrect regular expression that happens to have an alternation symbol in it – jvilhena Apr 28 '20 at 18:08

0 Answers0