Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

search and replace desired portion from a line

by syedasadali95 (Acolyte)
on May 29, 2019 at 07:36 UTC ( [id://11100686]=perlquestion: print w/replies, xml ) Need Help??

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

Hi monks, I am trying to search and replace a desired portion from a line using perl but not able to match regex to the end of a word. let me explain the issue below

Line:

0 (0) tracegen chn:req cmd:SDP_CMD_RDSIZED unit:0x10 addr:0x0 len:0x3f + vc:0 qospri:0 qosfw:0 strm:0x0 chain:0 io:0

Code:

$line1 =~ s/addr\:0x.*\S/addr\:$address/;

Result:

0 (0) tracegen chn:req cmd:SDP_CMD_RDSIZED unit:0x10 addr:0x500000000

Unfortunately my code is trimming the rest of the line after addr field which shouldn't happen.

expected result:

0 (0) tracegen chn:req cmd:SDP_CMD_RDSIZED unit:0x10 addr:0x500000000 +len:0x3f vc:0 qospri:0 qosfw:0 strm:0x0 chain:0 io:0

Please suggest a solution to match next white space after addr field or to match end of string and replace. I've tried $, \s, \S, \z, \', etc. But doesn't worked well

2019-05-30 Athanasius added code tags

Replies are listed 'Best First'.
Re: search and replace desired portion from a line
by Eily (Monsignor) on May 29, 2019 at 07:58 UTC

    .* means "go as far as you can using any character" so of course that matches the whole string (and \S matches the last non space character). If you only want non space chars after 0x you can use: 0x\S* instead .

      Thanks Eily, this was the most simplest solution
Re: search and replace desired portion from a line (updated)
by AnomalousMonk (Archbishop) on May 29, 2019 at 15:44 UTC

    Another statement of the problem might be "after 'addr:0x', replace 1 or more hex digits (or maybe just a single '0'?) with a string from a variable". The s/// match pattern might be expressed as
        (?<= addr:0x)  # after a certain literal string
        [[:xdigit:]]+  # 1 or more hex digits
    (Update: See "(?<=pattern)" (and friends) in Extended Patterns, "Lookaround Assertions" subsection, in perlre; Looking ahead and looking behind in perlretut. See also POSIX Character Classes in perlrecharclass for [[:xdigit:]] and similar.)

    c:\@Work\Perl\monks>perl -wMstrict -le "my $s = qq{0 (0) tracegen chn:req cmd:SDP_CMD_RDSIZED unit:0x10 addr: +0x0 len:0x3f vc:0 qospri:0 qosfw:0 strm:0x0 chain:0 io:0\n}; ;; my $address = '500000000'; ;; $s =~ s{ (?<= addr:0x) [[:xdigit:]]+ }{$address}xms; ;; my $expected = qq{0 (0) tracegen chn:req cmd:SDP_CMD_RDSIZED unit:0x1 +0 addr:0x500000000 len:0x3f vc:0 qospri:0 qosfw:0 strm:0x0 chain:0 io +:0\n}; print 's/// ok' if $s eq $expected; " s/// ok

    If you have Perl version 5.10+, you can use the more flexible  \K (see Extended Patterns) positive look-behind assertion operator:
        $s =~ s{ addr:0x \K [[:xdigit:]]+ }{$address}xms;
    This allows the positive look-behind pattern to be held in a variable and managed separately:
        my $tag = qr{ addr:0x }xms;
        $s =~ s{ $tag \K [[:xdigit:]]+ }{$address}xms;
    (Update: The advantage of such separate management is that a more complex pattern may be built up:
         my $tag = qr{ (?: addr | foo | bar) :0x }xms;)


    Give a man a fish:  <%-{-{-{-<

      Thanks for the detailed explanation
Re: search and replace desired portion from a line
by thanos1983 (Parson) on May 29, 2019 at 08:42 UTC

    Hello syedasadali95,

    I suppose you want to match any single integer from 0-9 after addr:0x*? If this is the case why not simply like:

    $line1 =~ s/addr:0x[0-9]/addr:$address/;

    Also there is no need to escape semicolon character. :)

    Update: Fellow Monk AnomalousMonk has a point. This is the more correct solution $line1 =~ s/addr:0x[[:xdigit:]]+/addr:$address/;. Since this a hex value and not just a digit.

    Hope this helps, BR.

    Seeking for Perl wisdom...on the process of learning...not there...yet!
      Thankyou

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2024-04-19 06:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found