Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Regarding regex and the * and ? Quantifiers

 
Greenhorn
Posts: 21
Firefox Browser C++ Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello all,

Why does the regex: abc[xyz]*def match the string abcxyzdef when the regex abc[xyz]?def fails to match the same string?

The character class [xyz] means match any one of x or y or z. Then, why does [xyz]* match all three?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because * means "zero or more", so abc[xyz]*def means abc followed by zero or more of x, y or z, followed by def.

And ? means "exactly one of", so abx[xyz]?def means abc followed by exactly one of x, y or z, followed by def.

So: abcxyzdef = abc followed by 3 chars which are x, y or z, followed by def => matches the first regex (because 3 is equal to "zero or more"), but does not match the second (because 3 is not equal to "exactly one").
 
Sheriff
Posts: 22644
123
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesper de Jong wrote:And ? means "exactly one of", so abx[xyz]?def means abc followed by exactly one of x, y or z, followed by def.


Wrong. ? means exactly one or none at all. For abc[xyz]?def there are 4 possible values: abcdef, abcxdef, abcydef and abczdef.
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, my mistake Rob!
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Spoor wrote:Wrong. ? means exactly one or none at all. For abc[xyz]?def there are 4 possible values: abcdef, abcxdef, abcydef and abczdef.


And then of course there's '[whatever]*?'...

Winston
 
There are 29 Knuts in one Sickle, and 17 Sickles make up a Galleon. 42 tiny ads in a knut:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic