extract strings between alternating text

Discussion in 'Perl' started by Lydia Shawn, Aug 10, 2003.

  1. Lydia Shawn

    Lydia Shawn Guest

    hi there,

    i want to extract the numbers from this example

    input:

    bla trigger3 trigger4 trigger1 blabla trigger1 5000.00 trigger3
    trigger1 trigger2 trigger2 600.00 trigger4
    trigger1 50.00 trigger4

    i want to extract the numbers everytime they occur between trigger1 or
    2 and trigger3 or 4.

    so output:
    5000,00
    600,00
    50,00


    i thought i could use something like this

    $return =~ /($trigger1|trigger2)(.*)(trigger3|trigger4)/si ;
    but obviously i can't.. cause it doesn't work..
    your ideas are very welcome!
    thanks!!

    lydia
     
    Lydia Shawn, Aug 10, 2003
    #1
    1. Advertising

  2. -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    (Lydia Shawn) wrote in
    news::

    > hi there,
    >
    > i want to extract the numbers from this example
    >
    > input:
    >
    > bla trigger3 trigger4 trigger1 blabla trigger1 5000.00 trigger3
    > trigger1 trigger2 trigger2 600.00 trigger4
    > trigger1 50.00 trigger4
    >
    > i want to extract the numbers everytime they occur between trigger1 or
    > 2 and trigger3 or 4.
    >
    > so output:
    > 5000,00
    > 600,00
    > 50,00
    >
    >
    > i thought i could use something like this
    >
    > $return =~ /($trigger1|trigger2)(.*)(trigger3|trigger4)/si ;
    > but obviously i can't.. cause it doesn't work..


    Well, think about it. In words, that pattern matches:

    The variable $trigger1 *or* the string "trigger2"
    followed by as much text as possible
    followed by the string "trigger3" or "trigger4".

    So it would match pretty much the whole string, eh?

    You want to match

    The string "trigger1" or "trigger2"
    followed by possible whitespace
    followed by digits (and maybe a decimal point?)
    followed by more possible whitespace
    followed by the string "trigger3" or "trigger4"

    right?


    /(trigger1|trigger2)
    \s*
    ([\d.]+)
    \s*
    (trigger3|trigger4)/six;

    The most important thing when writing regular expressions is to state
    *precisely* what you're looking for, and then translate it into small
    chunks that correspond to what Perl's RE engine can do, and then write
    the expression.

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

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

    iQA/AwUBPzWfKWPeouIeTNHoEQJXXwCg9yY/GXb8OXYXVVjlTtOL7QOA5/kAoJZU
    LA2duAEVvyDkVmEZcIX0tcHq
    =nJA/
    -----END PGP SIGNATURE-----
     
    Eric J. Roode, Aug 10, 2003
    #2
    1. Advertising

  3. Lydia Shawn

    Sam Holden Guest

    On 10 Aug 2003 02:56:10 -0700, Lydia Shawn <> wrote:
    > hi eric,
    >
    >> You want to match
    >> The string "trigger1" or "trigger2"
    >> followed by possible whitespace
    >> followed by digits (and maybe a decimal point?)
    >> followed by more possible whitespace
    >> followed by the string "trigger3" or "trigger4"

    >
    > exactly right! the variable was a mistake in my earlier posting but
    > you got it anyway!
    >
    > for some reason though the | doesn't seem to do it's job..
    >
    >=~ /(trigger1|trigger2)\s*([\d.]+)\s*(trigger3|trigger4)/six;
    >
    > returns "trigger1"


    Did you bother checking what was in $2?

    It isn't magic you know, the documentation tells you what each
    character in a regular expression does. You could try reading some
    of it.

    --
    Sam Holden
     
    Sam Holden, Aug 10, 2003
    #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. John

    Alternating text

    John, Sep 3, 2005, in forum: HTML
    Replies:
    12
    Views:
    1,188
    Stewart Gordon
    Sep 6, 2005
  2. MangroveRoot

    alternating text/image rows

    MangroveRoot, Nov 4, 2007, in forum: HTML
    Replies:
    7
    Views:
    427
    Adrienne Boswell
    Nov 4, 2007
  3. eric
    Replies:
    1
    Views:
    114
    Dilip Krishnan
    Jan 24, 2005
  4. Lydia Shawn

    extract strings between alternating text

    Lydia Shawn, Aug 10, 2003, in forum: Perl Misc
    Replies:
    5
    Views:
    119
    Tad McClellan
    Aug 10, 2003
  5. Mladen
    Replies:
    5
    Views:
    176
    Peter Scott
    Feb 22, 2011
Loading...

Share This Page