Win a copy of Terraform in Action this week in the Cloud 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

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: 22510
122
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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic