newbie: Problem with $ and \ in strings

Discussion in 'Perl Misc' started by vivek_12315, Apr 9, 2013.

  1. vivek_12315

    vivek_12315 Guest

    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 ?
    vivek_12315, Apr 9, 2013
    #1
    1. Advertising

  2. vivek_12315 <> 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
    Jürgen Exner, Apr 9, 2013
    #2
    1. Advertising

  3. Ben Morrow wrote:
    >
    > Quoth 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.

    >
    > 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
    John W. Krahn, Apr 12, 2013
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kurt Krueckeberg
    Replies:
    2
    Views:
    697
    =?ISO-8859-1?Q?Ney_Andr=E9_de_Mello_Zunino?=
    Nov 17, 2004
  2. Rick

    Comparing strings from within strings

    Rick, Oct 21, 2003, in forum: C Programming
    Replies:
    3
    Views:
    369
    Irrwahn Grausewitz
    Oct 21, 2003
  3. Klaus Neuner
    Replies:
    7
    Views:
    475
    Klaus Neuner
    Jul 26, 2004
  4. Girish Sahani
    Replies:
    17
    Views:
    561
    Boris Borcic
    Jun 9, 2006
  5. Ben

    Strings, Strings and Damned Strings

    Ben, Jun 22, 2006, in forum: C Programming
    Replies:
    14
    Views:
    740
    Malcolm
    Jun 24, 2006
Loading...

Share This Page