Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: Regex Help

by davido (Cardinal)
on Mar 25, 2013 at 08:28 UTC ( [id://1025251]=note: print w/replies, xml ) Need Help??


in reply to Regex Help

More than looking inefficient, it looks unnecessarily cluttered, which can contribute to bugs:

  • [\D]? is the same as \D?. This is repeated four times.
  • [0|1] is probably a mistake; character classes don't use alternation, and I don't see any | characters in your sample input. You probably mean [01]
  • \D?\d{4} is repeated twice in a row. How about (?:\D?\d{4}){2} ?

Making those changes would yield:

9818\D?9[01]\D?\d{2}(?:\D?\d{4}){2}

Now with the /x modifier, you can further clarify things like this:

m/ 9818 # A literal. \D? # Optional non-digit. [01] # Require a zero or a one. \D? # Another optional non-digit. \d{2} # Require two digits. (?: # Group but don't capture. \D? # Another optional non-digit. \d{4} # Followed by four digits. ){2} # Repeated twice. /x

As for efficiency, what problems are you encountering? If you're dealing with huge input you're probably IO bound anyway.


Dave

Replies are listed 'Best First'.
Re^2: Regex Help
by gautamparimoo (Beadle) on Mar 25, 2013 at 08:48 UTC

    Thnks davido your regex just cleaned it up. But what modifiers or assertion should i use to limit matching such that it does not match it in different lines in a text file ie only match if this pattern is specified in one line not across different lines. Pl tell?

      Replace \D? with a more explicit character class. \D will match anything that is not a numeric digit. Newlines (\n) are included in "anything that is not a numeric digit".


      Dave

        Exactly what i was trying to say.. How to do tell it to \D except \n ?

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2024-04-24 08:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found