Regex bug ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • BMermuys

    #1

    Regex bug ?

    Hi,

    string test="dit is een test";
    bool bMatch = Regex.IsMatch( test, "^([^wi]|[i])*$" );

    after executing, the bMatch is false

    The pattern is not optimized and seems a little stupid. But for large regex
    expressions using smaller strings to build it, the same problem could arise.

    First there is a character group which includes all character except 'wi',
    but then there's an or with an 'i' character.

    So as net result, only the w shouldn't be matched, the input string doesn't
    contain a 'w', so it should match.

    In perl this matches.

    Can anyone conform this is a bug or I'm missing something ?

    TIA
    Greetings


  • Brian Davis

    #2
    Re: Regex bug ?

    I have seen this problem with alternation before. To get around it, you can
    use grouping parentheses for each part of the alternation:

    ^(([^wi])|([i]))*$

    Note that this may change your result if you reference unnamed groups. I
    don't know why it works that way, but it definitely appears to be a bug in
    ..NET regular expressions.

    Brian Davis





    "BMermuys" <bmermuys_nospa m_@hotmail.com> wrote in message
    news:fsJub.3163 2$gF3.1112181@p hobos.telenet-ops.be...[color=blue]
    > Hi,
    >
    > string test="dit is een test";
    > bool bMatch = Regex.IsMatch( test, "^([^wi]|[i])*$" );
    >
    > after executing, the bMatch is false
    >
    > The pattern is not optimized and seems a little stupid. But for large[/color]
    regex[color=blue]
    > expressions using smaller strings to build it, the same problem could[/color]
    arise.[color=blue]
    >
    > First there is a character group which includes all character except 'wi',
    > but then there's an or with an 'i' character.
    >
    > So as net result, only the w shouldn't be matched, the input string[/color]
    doesn't[color=blue]
    > contain a 'w', so it should match.
    >
    > In perl this matches.
    >
    > Can anyone conform this is a bug or I'm missing something ?
    >
    > TIA
    > Greetings
    >
    >[/color]


    Comment

    • Jhon

      #3
      Re: Regex bug ?


      Hi,

      "Brian Davis" <@> wrote in message
      news:uhPj1JsrDH A.1060@TK2MSFTN GP12.phx.gbl...[color=blue]
      > I have seen this problem with alternation before. To get around it, you[/color]
      can[color=blue]
      > use grouping parentheses for each part of the alternation:
      >
      > ^(([^wi])|([i]))*$[/color]

      Yes, I noticed, but the fun part is, that it only works for capturing
      groups, if you would embed it in non-capturing groups(?:), it fails again...
      [color=blue]
      >
      > Note that this may change your result if you reference unnamed groups. I
      > don't know why it works that way, but it definitely appears to be a bug in
      > .NET regular expressions.[/color]

      I hope it gets fixed.

      Thanks for the reply
      greetings
      [color=blue]
      >
      > Brian Davis
      > www.knowdotnet.com
      >
      >
      >
      >
      > "BMermuys" <bmermuys_nospa m_@hotmail.com> wrote in message
      > news:fsJub.3163 2$gF3.1112181@p hobos.telenet-ops.be...[color=green]
      > > Hi,
      > >
      > > string test="dit is een test";
      > > bool bMatch = Regex.IsMatch( test, "^([^wi]|[i])*$" );
      > >
      > > after executing, the bMatch is false
      > >
      > > The pattern is not optimized and seems a little stupid. But for large[/color]
      > regex[color=green]
      > > expressions using smaller strings to build it, the same problem could[/color]
      > arise.[color=green]
      > >
      > > First there is a character group which includes all character except[/color][/color]
      'wi',[color=blue][color=green]
      > > but then there's an or with an 'i' character.
      > >
      > > So as net result, only the w shouldn't be matched, the input string[/color]
      > doesn't[color=green]
      > > contain a 'w', so it should match.
      > >
      > > In perl this matches.
      > >
      > > Can anyone conform this is a bug or I'm missing something ?
      > >
      > > TIA
      > > Greetings
      > >
      > >[/color]
      >
      >[/color]


      Comment

      Working...