Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: problem with regex

by stefp (Vicar)
on Jun 08, 2002 at 04:50 UTC ( [id://172742]=note: print w/replies, xml ) Need Help??


in reply to problem with regex to extract netstat info

I have a Unix box so I can't test the regexp after modification. So I have to better explain the motivations behind the modified regex if it needs further tweaking.

First avoid .* that is greedy and leads to uneeded backtracking or may lead to match more than intended. .*? is the non greeedy equivalent. Better, be more specific in your regexps. Here, using \S+ make you sure to capture only non blanks.

I have added the x modifier to your regexp to vertically align your regexp with mine. Also, I don't know the format output from your netstat so I defensively added \s* around colons in case there is indeed blanks around colons in the input. They may not be needed.
Also I used (\S+) to make clear that I expect non empty capture. If some capture may be empty use (\S*) instead.

/^\s+(.*) \s+(.*) : (.*)\s+ (.*) : (.*)\s+ (.*)/x /^\s+(\S+)\s+(\S+)\s*:\s*(\S+)\s+(\S+)\s*:\s*(\S+)\s+(\S+)/

Then there is the temptation to factorize:

my $qr = q|(\S+)\s+(\S+)\s*:\s*(\S+)|;

The regexp would become: <code>/^\s+$qr\s+$qr\s+(\S+)/<code>

But you can't do it because the captures done by $qr are invisible from the regexp where it is interpolated.

-- stefp -- check out TeXmacs wiki

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2024-04-19 04:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found