Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Regular Expression Question

by Roy Johnson (Monsignor)
on Jul 21, 2004 at 23:41 UTC ( [id://376418]=note: print w/replies, xml ) Need Help??


in reply to Regular Expression Question

Note that a question mark after a quantifier ({2}?) does not mean optional, but rather non-greedy. You need to put parens around the sub-expression to make it optional:
/^(\d{6}\.?(?:\d{2})?\w?$/;
If the optionals aren't independent, you may need to nest them:
/^(\d{6} #leading digits (?:\.? #if dot, then (?:(?:\d{2})? #if two digits, then \w?)))$/x; #maybe a character
Note the use of anchors, to prevent matching part of the name.

We're not really tightening our belts, it just feels that way because we're getting fatter.

Replies are listed 'Best First'.
Re^2: Regular Expression Question
by MistaMuShu (Beadle) on Jul 22, 2004 at 19:02 UTC
    Sorry, but I think when you say "use of anchors", it means the ^ and $ sign correct?

    Yeah, I guess I oversimplified the expression I wanted to search for, but this has been most helpful. Thanks.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://376418]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2024-04-25 08:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found