Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: regex on a line

by Basilides (Friar)
on Aug 01, 2002 at 16:40 UTC ( [id://186829]=note: print w/replies, xml ) Need Help??


in reply to regex on a line

There're a quiet a few bugs in this, and I think you'd really benefit from putting use strict at the top of your programs.

Anyway, you're missing a curly bracket, and you've spelt "view" wrongly in your print statement, so you'd never get a proper result.

Also, the string you're trying to match: "(view: pa_Iden etc etc)" has got spaces in it, so you'll never succeed in your match if you use the non-whitespace character, \w. You could substitute that for "." which matches anything, although I have heard monks say that ".*" is bad practice (I'm still quite a novice tho', so I don't know why they say this!).

Finally, when you assign to $view, you mean $2, not $1. You could do with taking out some of those brackets, which are confusing you with backreferences.

Here's a version which works:

while(<check>) { if (m/^(?:Update Status:)\s*(\w+)/) { $target = $1; } if ($target eq "open") { if (m/^(?:Update Status:)\s*(\w+)\s*(.*)$/) { $view = $2; } print "$target\n"; print "$view\n"; close(check); } }
However, here's a slightly more concise version which only uses one regex:
while(<check>) { if (m/^(?:Update Status:)\s*(\w+)\s*(.*)$/) { if ($1 eq "open") { $view = $2; print "$view\n"; close(check); } } }
Note that in each of these, as soon as an "Update: open" line is found, the file is closed. I don't know if that's what you want, but if not, you'll have to move that close(check); line.

HTH
Dennis

Log In?
Username:
Password:

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

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

    No recent polls found