Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
For example in a file if I see:
"<start> open:my_file.txt open:other_file.text </start>"
I'd like to replace it with
"<start> {open_me:my_file.txt} {open_me:other_file.text} </start>"
I try this but it doesn't match anything:
($str is the string I'm searching in)
$str =~ s/^open_me:(.*)\.(txt|text)$/{open_me:$1\.$2}/g
I used the '\' because I wanted to match a literal dot. I have the anchors to match multiple instances on the same line(like in the example). The text I want to search and replace in can be surrounded by any text, which is another reason I have the anchors. Thanks.

The ^ and $ anchors ANCHOR the match to the START and END of the string. They are not used for matching multiple instances of the pattern. Your regex will only match if you DON'T have anything before open_me or after (txt|text).
Take the anchors out.

I suggest you pick up a copy of "Mastering Regular Expressions".
http://www.oreilly.com/catalog/regex/

regexp is very useful if you know how to use it. Here's one that doesn't use regexp, but it's in Python. i guess you could do that too in Perl.
o = open("outputfile.txt","a")
for lines in open("inputfile.txt"):
.......if "<start>" in lines:
..............line = lines.split()
..............o.write(line[0])
..............for i in line[1:-1]:
...................o.write("{%s}" % (i) )
..............o.write(line[-1])
...
o.close()output is <start>{open:my_file.txt}{open:other_file.text}</start>

########################################################################
$str =~ s/open:my(.*?)\.(te?xt) open:other(.*?)\.(te?xt)/{open_me:$1\.$2} {open_me:$3.$4}/;
or, we should be able to simplify it to this:
s/open:(\S+)/{open_me:$1}/g;

If I am using "$_=~ s/" and "/g" to search and replace multiple instances on one line, how do I extract all those replacements? Thank you.

I ask because I found out s/ doesnt actually go into the file and do the search and replace(like sed), so I need the new string to use File I/O and actually edit the file.

I'm not sure what you mean by "how do I extract all those replacements?".
Can you post the relevant portion of your script and a reasonable sample of the before and after of the file you're trying to edit?

One situation(From first post):
"<start> open:my_file.txt open:other_file.text </start>"
I'd like to replace it with
"<start> {open_me:my_file.txt} {open_me:other_file.text} </start>"
Second Situation:
"<start> open:my_file.txt</start>"
I'd like to replace it with
"<start> {open_me:my_file.txt}"I need s/ to actually edit the line inside the text file I am reading from appropriately, no matter how many matches there are in that line. So my questions are:
1. s/ doesnt affect the file you are scanning, so is additional file I/O required to search and replace stuff in the file being scanned for this regex?
2. What is the regex?
Thanks.

Are you trying to do this from the command line or from a script?
You need to read the file line-by-line and pass the line to the regex, which can be done from the command line or within a script.
Here's how you can do an inline edit of your file from the command line.
perl -ni.bak -e 's/open:(\S+)/{open_me:$1}/g' somefile
There are several ways to do it within a script, here's one approach.
#!/usr/bin/perl
open (IN, "srcfile") || die $!;
open (OUT, ">destfile") || die $!;while (<IN>) {
s/open:(\S+)/{open_me:$1}/g;
print OUT;
}
close IN;
close OUT;Both of those methods will replace each occurrence of the match on each line (the g modifier on the regex is what allows it to make multiple matches/substitutions per line). The command line example will create a backup copy of the original file (adding the .bak ext) and the edited file retains the original filename. The second example keeps the original file intact and creates a new file with a new name of your choice.
Without seeing your source file, I can't say for sure if these will do exactly what you want/need. It is possible that the regex will need an adjustment, but given the example line you posted, each method should work.

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |