Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > extract strings between alternating text

Reply
Thread Tools

extract strings between alternating text

 
 
Lydia Shawn
Guest
Posts: n/a
 
      08-10-2003
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
 
Reply With Quote
 
 
 
 
Eric J. Roode
Guest
Posts: n/a
 
      08-10-2003
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

(Lydia Shawn) wrote in
news: om:

> 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-----
 
Reply With Quote
 
 
 
 
Sam Holden
Guest
Posts: n/a
 
      08-10-2003
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
 
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
Please help me how is easiest way to extract text between some variable text Mladen Perl Misc 5 02-22-2011 10:57 AM
alternating text/image rows MangroveRoot HTML 7 11-04-2007 04:34 PM
Alternating text John HTML 12 09-06-2005 04:40 PM
Alternating between calling webservice in Java and in .Net. eric ASP .Net Web Services 1 01-24-2005 02:33 PM
extract strings between alternating text Lydia Shawn Perl Misc 5 08-10-2003 01:42 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57