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!?
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.