Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Regex Question

by talexb (Canon)
on Sep 10, 2013 at 18:07 UTC ( #1053329=note: print w/ replies, xml ) Need Help??


in reply to Regex Question

When dealing with regular expressions, my approach is to do the simplest thing that works.

In your case, this means looking for three capital letters, following by some stuff, and then a two or three digit number, a dash, and the same two or three digit number.

So the regexp I'd use would be

/([A-Z]{3}).+(\d{2,3})-\2/
This makes the assumption that the two numbers at the end are the same. If they might be different, you'd have to change the second capture to something like
/([A-Z]{3}).+(\d{2,3}-\d{2,3})/
When in doubt, try the simplest thing that could work. You're putting 'Protocol' and other stuff in there -- throw it away -- you don't need it. :)

Alex / talexb / Toronto

"Groklaw is the open-source mentality applied to legal research" ~ Linus Torvalds


Comment on Re: Regex Question
Select or Download Code
Re^2: Regex Question
by Anonymous Monk on Sep 10, 2013 at 18:30 UTC
    ... and/or do it in stages: write one regex to grab an entire-line and then a second regex to break down what the first one grabbed.
Re^2: Regex Question
by clegane (Novice) on Sep 10, 2013 at 18:32 UTC

    Talexb, thanks for the reply. Please see my other reply to keszler. The protocol and destination ports are in there b/c that's how I can pick them out. The data can follow this structure:

    something, Protocol: UDP, something, Destination Port: x-y, something, something, Protocol: TCP, something, Destination Port: a-b, something

    So, reliably picking them out can be challenging without the field names. Also, this data I'm working with doesn't always follow this format, so I'm needing to ignore the data that doesn't.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (8)
As of 2014-08-23 13:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (173 votes), past polls