2

As long as I know a little about RegExp, the two below patterns are the same:

`^(?:[ab]|ab)(.)(?:(?!\1).)+\1$`

`^(?:ab|[ab])(.)(?:(?!\1).)+\1$`

but the number [ 1 ] is false and the [ 2 ] is true, whereas it should be true for both:


code:

void main( immutable string[] args ){

    immutable string str = "ab some-word ";
    Regex!( char ) rx = regex( `^(?:[ab]|ab)(.)(?:(?!\1).)+\1$` );
    immutable bool     b1 = !matchFirst( str, rx ).empty();

    writeln( b1 );  // false ( should be true )

    rx = regex( `^(?:ab|[ab])(.)(?:(?!\1).)+\1$` );
    immutable bool     b2 = !matchFirst( str, rx ).empty();

    writeln( b2 );  // true

}

Demo on regex101.com

the main problem is not related to character class [], since the following is true for both:

^(?:ab|[ab])(.)-\1$

^(?:[ab]|ab)(.)-\1$

but with: (.)(?:(?!\1).) it fails if a character-class appears at the beginning.

I am not sure but may it is the same bug that GCC below the version 5.3.0 have had.

here is my question and found out this bug:
the same regex but different results on Linux and Windows only C++

Please correct me if I am wrong

Community
  • 1
  • 1
Shakiba Moshiri
  • 21,040
  • 2
  • 34
  • 44

0 Answers0