Difference between inanimate search and negative character set

Is there a difference between the two regex patterns (if single line mode is enabled): a.*?b and a[^b]*b ? What about performance?

+6
source share
2 answers

a.*?b should check for every character consumed if it matches the pattern (i.e. if the next is b ). This is called a retreat.

Using the a12b line a12b execution will look like this:

  • Consume a
  • Use the following 0 characters. Is the next a b ? No.
  • Use the following character ( a1 ). Is the next a b ? No.
  • Use the following character ( a12 ). Is the next a b ? Yes!
  • Consume b
  • Match

a[^b]*b consumes everything that is not b without asking itself questions, and because of this it is much faster for longer strings.

Using the a12b line a12b execution will look like this:

  • Consume a
  • Consume all that follows; it is not b . ( a12 )
  • Consume b
  • Match

RegexHero has a reference function that demonstrates this using the .NET regex engine.

Besides the performance difference, they correspond to the same lines in your example.

However, there are situations when there is a difference between them. In line aa111b111b

(?<=aa.*?)b matches as b , and (?<=aa[^b]*)b matches only the first.

+4
source

I tested your both regular expressions, calling them like:

 NONGREEDY = /a.*?b/; GREEDY = /a[^b]*b/; 

I named the negative regex as GREEDY, but it's just a name.

You can test test-non-greedy-vs-greedy-performance on JsPerf and run the tests to see them yourself. Feel free to modify the string to perform various test cases.

You can check out the various tests that the guys added, and the test results vary depending on the input line.

Below is test for string: ab

enter image description here

enter image description here

Below is the test for the string: axb enter image description here

Below is test for string: afdkjsklfjsdlkfjsdlkfjsdlkjflskdjflsdfjjflksdjfb enter image description here

After these tests, performance seems to change depending on the line you are playing.

Hope this test can help answer this question.

+1
source

Source: https://habr.com/ru/post/972053/


All Articles