Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Regex: Changing $1

by packetstormer (Monk)
on Sep 04, 2013 at 14:16 UTC ( [id://1052347]=perlquestion: print w/replies, xml ) Need Help??

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

Hello Monks

I really struggle with Regular Expressions! I have been battling this for a while but can't figure it

I need to match a string between two double quotes then make changes to that matched string. However, it is proving difficult. See below, I capture the line between quotes into a variable then make changes to it. I then want to change that back in the line (to write out to another file later)

#!/usr/bin/perl use strict; open (FH,"test.test") or die; while(<FH>) { if ($_ =~ /"(.+?)"/) { print "Matched: $1\n"; } }
test.test looks like this:
This is line 1 "hello there" the is line 2 "goodbye, take care" this is line 3 "Jump, fox. Jump"
output:
Matched: hello there Matched: goodbye, take care Matched: Jump, fox. Jump
How do I modify $1 back into $_ to write out? I have tried assigning $1 to $new and then matching that on the original string but I can't figure it out!?

Replies are listed 'Best First'.
Re: Regex: Changing $1
by toolic (Bishop) on Sep 04, 2013 at 14:25 UTC
    substitution
    use warnings; use strict; while (<DATA>) { s/"(.+?)"/"NEW"/; print; } __DATA__ This is line 1 "hello there" the is line 2 "goodbye, take care" this is line 3 "Jump, fox. Jump"

    prints:

    This is line 1 "NEW" the is line 2 "NEW" this is line 3 "NEW"
Re: Regex: Changing $1
by hdb (Monsignor) on Sep 04, 2013 at 14:28 UTC

    My understanding is that you want to modify the part of the string between the quotes? One way is to use the e flag in the substitution operator s/// to replace $1 with a modified version, in the following example with an upper case version:

    s/"(.+?)"/'"'.uc $1.'"'/e;
Re: Regex: Changing $1
by choroba (Cardinal) on Sep 04, 2013 at 14:25 UTC
    Just store what's around in other variables:
    if (my ($prefix, $matched, $suffix) = /^(.*?)"(.+?)"(.*)/) { $matched = ... # Do what you need. print qq($prefix"$matched"$suffix\n); }
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: Regex: Changing $1
by davido (Cardinal) on Sep 04, 2013 at 15:11 UTC

    A one-liner using the in-place edit switch.

    perl -pi.bak -e 's/^[^"]*"([^"]+?)"[^"]*$/$1/' filename

    This reads in the line, and drops everything that comes before the first quote, or after the second. It will fail if the string contains more than one set of balanced quotes. It then writes the part that came between the quotes back out to the original file, and leaves a backup in place.

    That's a slight lie, actually; If I recall -i.bak semantics, it renames the input file, and then writes a new file to the original input filename.

    You can mimic that approach in long-hand code as well; open the input file, rename it to something else to save it just in case, open a new output file to the name of the old input file, read each line, drop the parts you don't want, write each line out to the output file, done.

    But problems where you're matching text between quotes are seldom as simple as they seem at first. What if the string is, This is line 1 "Hello! I said, "Hello!""? Now you've got a problem. That problem is handled more easily using Text::Balanced.


    Dave

Re: Regex: Changing $1
by packetstormer (Monk) on Sep 04, 2013 at 15:38 UTC
    Thanks for all the replies - plenty of stuff for me to have a go at!
Re: Regex: Changing $1
by space_monk (Chaplain) on Sep 04, 2013 at 14:30 UTC
    Try
    open (FH,"test.test") or die; while(<FH>) { if ($_ =~ /"(.+?)"/) { print "Matched: $1\n"; s/$1/New string/; } print; }
    P.S. You can make a one liner out of this
    If you spot any bugs in my solutions, it's because I've deliberately left them in as an exercise for the reader! :-)

      Is problematic if $1 also matches outside of the quotes, e.g. test "test".

      Only works if $1 does not contain any special characters. Prepend \Q to "quote" all the special characters in $1.
      لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: Regex: Changing $1
by Anonymous Monk on Sep 04, 2013 at 14:44 UTC
    Grab all three pieces ... before, during, and after ... into $1, $2, $3. Reassemble the output string from $1 . $newstuff . $3. Test aggressively.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2024-03-19 05:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found