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

how to do replace more then one matches?

by virudinesh (Acolyte)
on Jun 03, 2013 at 07:46 UTC ( #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

Comment on how to do replace more then one matches?
Download Code
Re: how to do replace more then one matches?
by hdb (Parson) 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; }
Re: how to do replace more then one matches?
by 2teez (Priest) 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 AnomalousMonk (Monsignor) 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'

Log In?
Username:
Password:

What's my password?
Create A New User
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? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (10)
As of 2014-07-10 07:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (202 votes), past polls