Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > newbie: Problem with $ and \ in strings

Reply
Thread Tools

newbie: Problem with $ and \ in strings

 
 
vivek_12315
Guest
Posts: n/a
 
      04-09-2013
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 ?

 
Reply With Quote
 
 
 
 
Jürgen Exner
Guest
Posts: n/a
 
      04-09-2013
vivek_12315 <(E-Mail Removed)> wrote:
>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
 
Reply With Quote
 
 
 
 
John W. Krahn
Guest
Posts: n/a
 
      04-12-2013
Ben Morrow wrote:
>
> Quoth vivek_12315<(E-Mail Removed)>:
>> 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
--
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction. -- Albert Einstein
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Strings, Strings and Damned Strings Ben C Programming 14 06-24-2006 05:09 AM
How to generate k+1 length strings from a list of k length strings? Girish Sahani Python 17 06-09-2006 11:01 AM
Catching std::strings and c-style strings at once Kurt Krueckeberg C++ 2 11-17-2004 03:53 AM
convert list of strings to set of regexes; convert list of strings to trie Klaus Neuner Python 7 07-26-2004 07:25 AM
Comparing strings from within strings Rick C Programming 3 10-21-2003 09:10 AM



Advertisments