Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

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.

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?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1005389]
Approved by moritz
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (5)
As of 2017-04-23 08:12 GMT
Find Nodes?
    Voting Booth?
    I'm a fool:

    Results (430 votes). Check out past polls.