Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Regular Expression Assistance

by g_speran (Scribe)
on Jun 19, 2024 at 16:41 UTC ( [id://11160111]=perlquestion: print w/replies, xml ) Need Help??

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

Wondering if someone can put eyes on this and tell me what i am doing incorrectly. I've tried several option but make it worse

Hoping someone can quickly spot my error.

i need to get rid of the doublequotes after 'value=' and before '/>'

Thanks in advance

my $content = '<entry key="maxcompdatastripe" value="41943040" />'; my $maxreplacement_value = ' "20917520" merge="keep" '; $content =~ s/(<entry key="maxcompdatastripe" value=")[^"]*("\s*\/>)/$ +1$maxreplacement_value$2/g; print "Content: $content\n"; Expected results: <entry key="maxcompdatastripe" value="20917520" merge="keep" /> Actual Results: Content: <entry key="maxcompdatastripe" value=" "20917520" merge="keep +" " />

Replies are listed 'Best First'.
Re: Regular Expression Assistance
by choroba (Cardinal) on Jun 19, 2024 at 18:11 UTC
    XML is better handled by an XML-aware tool.
    #!/usr/bin/perl use warnings; use strict; use feature qw{ say }; use XML::LibXML; my $content = '<entry key="maxcompdatastripe" value="41943040" />'; my $entry = 'XML::LibXML'->load_xml(string => $content)->documentEleme +nt; $entry->{value} = '20917520'; $entry->{merge} = 'keep'; say $entry;

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
Re: Regular Expression Assistance
by jdporter (Paladin) on Jun 19, 2024 at 16:49 UTC
Re: Regular Expression Assistance
by g_speran (Scribe) on Jun 19, 2024 at 17:07 UTC

    I have figured this out

    $content =~ s/(<entry key="maxcompdatastripe" value)="([^"]*)"\s*\/>/$ +1=$maxreplacement_value\/>/;

Log In?

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2024-07-18 05:40 GMT
Find Nodes?
    Voting Booth?

    No recent polls found

    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.