Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

how to do replace more then one matches?

by virudinesh (Acolyte)
on Jun 03, 2013 at 07:46 UTC ( [id://1036683]=perlquestion: print w/replies, xml ) Need Help??

virudinesh has asked for the wisdom of the Perl Monks concerning the following question:

its replace was done again and again

$li ="abbjghfg table 1 vinoth figer table2"; $f=$li; while($f=~m /table[ ]([0-9])+|table([0-9])+|fig[ ][0-9]+|fig[0-9]+/i +) { $f = $'; #print " after ----$f \n "; $match_key=$&; #print " --match : --$match_key \n "; $front=$`; $rep=$front."<aid=".$match_key.$'; print" $rep \n"; }

output come

abbjghfg <aid=table 1 vinoth figer table2 vinoth figer <aid=table2

but i tried different ways but how to do like this i dont know

need like this abbjghfg <aid=table 1 vinoth figer <aid=table2

Replies are listed 'Best First'.
Re: how to do replace more then one matches?
by AnomalousMonk (Archbishop) on Jun 03, 2013 at 15:06 UTC

    Maybe something like (note the  s///g regex modifier):

    >perl -wMstrict -le "my $s = 'xyz table 1 table tablet table2 fig 3 fig figer fig4 zyx'; print qq{'$s'}; ;; $s =~ s{ (?= (?: table | fig) \s* \d+) }{<aid=}xmsg; # <-- note /g m +odifier print qq{'$s'}; " 'xyz table 1 table tablet table2 fig 3 fig figer fig4 zyx' 'xyz <aid=table 1 table tablet <aid=table2 <aid=fig 3 fig figer <aid=f +ig4 zyx'
Re: how to do replace more then one matches?
by 2teez (Vicar) on Jun 03, 2013 at 09:39 UTC

    You mean something like this?:

    use warnings; use strict; my $li = "abbjghfg table 1 vinoth figer table2"; my $new_str; while ( $li =~ m/(.+?)(table.?\d)/gc ) { $new_str .= $1 . '<aid=' . $2; } print $new_str, $/; #abbjghfg <aid=table 1 vinoth figer <aid=table2
    Update: Why to avoid using $&, $`, or $' in your program

    If you tell me, I'll forget.
    If you show me, I'll remember.
    if you involve me, I'll understand.
    --- Author unknown to me
Re: how to do replace more then one matches?
by hdb (Monsignor) on Jun 03, 2013 at 09:02 UTC

    I guess it is the $' at the end of your line $rep=$front."<aid=".$match_key.$';. I you remove it you get something like the desired output.

      $f variable r store in $'(after match)

      both r same

        $li ="abbjghfg table 1 vinoth figer table2"; $f=$li; while($f=~m /table[ ]([0-9])+|table([0-9])+|fig[ ][0-9]+|fig[0-9]+/i) { $f = $'; $match_key=$&; $front=$`; $rep=$front."<aid=".$match_key; print $rep; }

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (5)
As of 2024-04-20 00:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found