Grabbing certain section of a string

Discussion in 'Perl Misc' started by Desmo, Jul 9, 2003.

  1. Desmo

    Desmo Guest

    If I have a rather long string like "blah blah
    blah.....filename="intent.doc"...blah blah
    blah...filename="second.doc"....blah blah", and I want to assign the words
    between the quotation marks after filename= to variables, is there a quick
    and easy way of doing it?

    --

    Ryan Carrier
    ISA CCST III
    Fraser Papers, Inc.
    (207) 728-8601
    Desmo, Jul 9, 2003
    #1
    1. Advertising

  2. In article <>,
    "Desmo" <> wrote:

    > If I have a rather long string like "blah blah
    > blah.....filename="intent.doc"...blah blah
    > blah...filename="second.doc"....blah blah", and I want to assign the words
    > between the quotation marks after filename= to variables, is there a quick
    > and easy way of doing it?


    @filenames = $longstring =~ /"([^"]+)"/g;

    hth-
    --
    Michael Budash
    Michael Budash, Jul 9, 2003
    #2
    1. Advertising

  3. Desmo

    fatted Guest

    Abigail <> wrote in message news:<>...
    > Desmo () wrote on MMMDXCIX September MCMXCIII in
    > <URL:news:>:


    <snip>

    > Assuming the string is in $str:
    >
    > my @filenames = $str =~ /filename="([^"]*)"/g;


    Can you explain how the part of the regexp thats inside the () works.
    I just don't follow :(. Is there any reason not to use (.*?) {instead
    of what you have ([^"]*)}?
    fatted, Jul 10, 2003
    #3
  4. -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    (fatted) wrote in
    news::

    > Abigail <> wrote in message
    > news:<>...
    >> Desmo () wrote on MMMDXCIX September MCMXCIII in
    >> <URL:news:>:

    >
    > <snip>
    >
    >> Assuming the string is in $str:
    >>
    >> my @filenames = $str =~ /filename="([^"]*)"/g;

    >
    > Can you explain how the part of the regexp thats inside the () works.
    > I just don't follow :(. Is there any reason not to use (.*?) {instead
    > of what you have ([^"]*)}?
    >


    Speed. It's faster to greedily slurp up "as many non-quote characters
    as possible" than it is to non-greedily find "as few of any character as
    possible".

    When you use a greedy expression, the RE engine zips forward as far as it
    can, then backs off until the next atom matches. In the above case,
    it'll zip past all the non-quote characters and will match the next atom
    (quote), and so will not have to back off at all.

    When you use a non-greedy expression, the RE engine trods forward one
    matching character at a time, each time stopping to check if the next
    atom matches. Ih the above case, it'll match . (any character) and each
    time check to see if the next character is a quote, which most of the
    time it won't be.

    The actual situation inside the RE engine is a bit more complex, because
    it does some fancy optimizations here and there, and has a bit more
    smarts than I described above. But the above is conceptually correct,
    and is the way to think about greed vs non-greed if you want to write
    faster expressions.

    - --
    Eric
    $_ = reverse sort qw p ekca lre Js reh ts
    p, $/.r, map $_.$", qw e p h tona e; print

    -----BEGIN PGP SIGNATURE-----
    Version: PGPfreeware 7.0.3 for non-commercial use <http://www.pgp.com>

    iQA/AwUBPw3qWmPeouIeTNHoEQKW7ACg3RfO1WM0D1c+4/ogCRccLVoRUAsAoMDe
    COVQikVe3hvOhbuEf/PR28sl
    =rK1g
    -----END PGP SIGNATURE-----
    Eric J. Roode, Jul 10, 2003
    #4
    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. Maziar Aflatoun
    Replies:
    1
    Views:
    346
    =?Utf-8?B?bG9uZG9uIGNhbGxpbmc=?=
    Jul 5, 2005
  2. Replies:
    0
    Views:
    633
  3. Replies:
    2
    Views:
    502
    bruce barker
    Mar 25, 2008
  4. SAN CAZIANO
    Replies:
    8
    Views:
    162
    Dr John Stockton
    Oct 15, 2004
  5. kampy
    Replies:
    9
    Views:
    306
    Steven D'Aprano
    Oct 19, 2012
Loading...

Share This Page