extract strings between alternating text

L

Lydia Shawn

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
 
E

Eric J. Roode

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

(e-mail address removed) (Lydia Shawn) wrote in
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-----
 
S

Sam Holden

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.
 

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,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top