[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[ale] VI vs perl RE question



> Does + mean the same thing it does in grep/vi? That is, "one or more
> of the preceding expression"?  If so, it appears "?" can mean
> different things depending on how it's used: in the first example, above,
> it seems to mean "the previous thing is optional", whereas in
> the second example, it seems to mean "don't be greedy".

Perl regexen are a superset of the normal, traditional *NIX regexen.

? by itself as a qualifier means 0 or 1 of the preceding atom
* by itself as a qualifier means 0 or more of the preceding atom
+ by itself as a qualifier means 1 or more of the preceding atom

Additionally these may be followed by a ? to indicate that they should be
non-greedy and match the least number of characters which allows the whole
regex to still match.

?? means 0 or 1 of the preceding atom, preferably matching 0
*? means 0 or more of the preceding atom, matching the fewest possible
+? means 1 or more of the preceding atom, matching the fewest possible


Some examples:

  DB<1> $_ = "foooobar"

  DB<2> x /(fo+)/
0  'foooo'
  DB<3> x /(fo+?)/
0  'fo'
  DB<4> x /(fo*)/
0  'foooo'
  DB<5> x /(fo*?)/
0  'f'
  DB<6> x /(fo??)/
0  'f'
  DB<7> x /(fo?)/
0  'fo'

_Mastering Regular Expressions_ and "perldoc perlre" discuss greedy-ness
vs non-greedy if you've still got questions.

-- 
Fletch                | "If you find my answers frightening,       __`'/|
fletch at phydeaux.org|  Vincent, you should cease askin'          \ o.O'
                      |  scary questions." -- Jules                =(___)=
                      |                                               U