Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

need help to replace string in a line

by vkp (Novice)
on Nov 24, 2012 at 16:07 UTC ( #1005389=perlquestion: print w/ replies, xml ) Need Help??
vkp has asked for the wisdom of the Perl Monks concerning the following question:

hi there, i need to replace some string in a line. The string could be (for example) nf=6 or nfabc = 7 or nf = {(3)*(3)} or nfabc = {(1)*(1)}. I am using following

$_=~/ +nf.*? *\=? *\d+/

for nf =4 or nfabc=4. and second is

 $_=~/ +nf.*? *\= *.*?\}/

for nf or nfabc ={} like stuff. The second is working fine. But the first is also targeting the {} stuff and it is taking pattern like nf={4} g=4 matched, and returning $& as nf={4} g=4. I know it is because of \d+ in last. But i want to make first line only to match nf=4 or nfabc=4. Not to {} stuff, because it means different for me. Please modify first code line so that i can get correct match. thanks.

Comment on need help to replace string in a line
Select or Download Code
Replies are listed 'Best First'.
Re: need help to replace string in a line
by moritz (Cardinal) on Nov 24, 2012 at 16:22 UTC

    I haven't checked it, but I guess your extensive use of .*? is causing trouble. That can match lots of stuff you don't want it to match. Be more specific!

    For example:

    / nf\w* \h* = \h* \d+ /x # ^ don't make the = optional # ^^^ not arbitrary characters, just allow word characters / nf\w+ \h* = \h* \{ [^}]+ \}/ # ^^^^ don't allow a closing bracket inside the bra +ckets

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (9)
As of 2015-07-30 17:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (273 votes), past polls