Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

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?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (5)
As of 2025-06-17 17:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.