newbie: Problem with $ and \ in strings

V

vivek_12315

I m parsing a line like:

line = [feature-tributary/access_db.wxs:35: <File Name="EmptyDB.mdb" Source="$(env.ARCHIVE_DIRECTORY)\access_db\DS Apps\Template\Database\FILE.mdb" KeyPath="yes" DiskId="2" Checksum="yes" Id="a621e7596dfcc45ffaec5fe2bb a84a6f1" />]

Contents are in square brackets.

I just want to extract the file name with is in the value of Source attribute.
i.e. FILE.mdb

I tried doing

1. if ($line =~ m/(.*)Source="(.*)"\sKeyPath(.*)/) {

2. if ($line =~ m/(.*)Source="(.*)\.(.*)"(.*)/o) {

3. if ($line =~ m/(.*)Source="(.*)"(.*)/o) {

but none of them is giving me what is required. Even the $ sign in $env is messing out the output when i print on console.

Can someone help me or give pointers ?
 
J

Jürgen Exner

vivek_12315 said:
I m parsing a line like:

line = [feature-tributary/access_db.wxs:35: <File Name="EmptyDB.mdb" Source="$(env.ARCHIVE_DIRECTORY)\access_db\DS Apps\Template\Database\FILE.mdb" KeyPath="yes" DiskId="2" Checksum="yes" Id="a621e7596dfcc45ffaec5fe2bb a84a6f1" />]

Contents are in square brackets.

I just want to extract the file name with is in the value of Source attribute.
i.e. FILE.mdb

Try
$line =~ m/Source="(.*?)"/
print $1;

The additional '?' in the RE changes '.*' from trying to match the
longest possible substring to matching the shortest possible substring,
AKA non-greedy matching.

jue
 
J

John W. Krahn

Ben said:
Quoth vivek_12315 said:
I m parsing a line like:

line = [feature-tributary/access_db.wxs:35:<File
Name="EmptyDB.mdb" Source="$(env.ARCHIVE_DIRECTORY)\access_db\DS
Apps\Template\Database\FILE.mdb" KeyPath="yes" DiskId="2" Checksum="yes"
Id="a621e7596dfcc45ffaec5fe2bb a84a6f1" />]

Contents are in square brackets.

Is this actually all on one line, or are there intervening newlines?
This makes a difference to the /./ regex character: if you don't use /s,
it won't match a newline. (My patterns below don't use /./, so are
unaffected by this.)
I just want to extract the file name with is in the value of Source attribute.
i.e. FILE.mdb

I tried doing

1. if ($line =~ m/(.*)Source="(.*)"\sKeyPath(.*)/) {

2. if ($line =~ m/(.*)Source="(.*)\.(.*)"(.*)/o) {

You don't ever want to use /o. Since perl 5.6 (a very long time ago)
perl precompiles all regexes, so /o will do no good and may do some
harm.

The /o option only applies if variables are interpolated in the regular
expression, but there are no variables in this case, so the /o option is
superfluous.


John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top