Need help with an advanced? regular expression

M

Martin Gill

Hi,

I'm trying to write a regular expression which parses the following string:

blah blah items 1234, 4567, 4345, and 3245 blah blah blah

I want to be able to pick up the numbers following the "items" label.

I thought the following might work, but it doesn't seem to

/ORs (\b(\d+)\b)+/

i want it to match:
1234
4567
4345
3245

Any help is greatly appreciated.
 
M

Martin Gill

Thanks for the quick reply,

replace ORs with items. I'm trying to use the regex in different places,
and I picked the other example.
What is that supposed to do?






With the input and specification you've provided this will work for
you:


print "$_\n" for m/(\d+)/g;

The problem I have is that the target string could be something like this:

Over the next 10 days i'll deliver 4 items 1234, 1234, 5321 and 2345.

I want to use items as the key phrase to identify the list of times.
The example you gave will also find 10 and 4 which i don't want.

In english, the regex i need is: Find all numbers after "items".
 
G

Gunnar Hjalmarsson

Martin said:
The problem I have is that the target string could be something like this:

Over the next 10 days i'll deliver 4 items 1234, 1234, 5321 and 2345.

I want to use items as the key phrase to identify the list of times.
The example you gave will also find 10 and 4 which i don't want.

In english, the regex i need is: Find all numbers after "items".

You don't necessarily need a pure regex, do you?

print "$_\n" for substr($_, index $_, 'items') =~ /\d+/g;
 
A

Arndt Jonasson

Martin Gill said:
The problem I have is that the target string could be something like this:

Over the next 10 days i'll deliver 4 items 1234, 1234, 5321 and 2345.

I want to use items as the key phrase to identify the list of times.
The example you gave will also find 10 and 4 which i don't want.

In english, the regex i need is: Find all numbers after "items".

I would first extract the substring beginning with "items" and then
apply the regexp to find the numbers.

Maybe it can be done in one single regexp (I don't think it can), but
even if so, would it be worth the effort?
 
J

jl_post

Martin said:
I'm trying to write a regular expression which parses the following
string:

blah blah items 1234, 4567, 4345, and 3245 blah blah blah

I want to be able to pick up the numbers following the "items" label.

I thought the following might work, but it doesn't seem to

/ORs (\b(\d+)\b)+/

i want it to match:
1234
4567
4345
3245


Well, for one thing, you want to pick up the numbers following the
"items" string, but in your regular expression you are searching for
"ORs" instead (which doesn't appear in your string at all).

If you have a $string variable:

$string = "blah blah items 1234, 4567, 4345, and 3245 blah blah blah";

you can print out all the numbers by first matching "items" and then by
matching all the numbers in the postmatch (the $' variable), like this:

if ($string =~ /items/)
{
# Everything after "items" (the postmatch) is now in $'
# so extract all the numbers in $' :
print "$_\n" foreach $' =~ m/\d+/g;
}

But be warned! Use of $' carries a performance penalty, making many of
Perl programmers avoid it. If this performace penalty bothers you, you
can avoid it with the following similar code:

if ($string =~ /items(.*)/)
{
# Everything after "items" is now in $1
# so extract all the numbers in $1 :
print "$_\n" foreach $1 =~ m/\d+/g;
}

If this is the only regular expression in your program, or if the
other regular expressions operate on relatively small strings, then
using $' should be nothing to worry about. In such cases, I think you
should use whatever method is more readable to you.

I hope this helps.

-- Jean-Luc
 

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,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top