2

I am attempting to parse a string in Perl with the format:

Messages pushed to the Order Book queues 123691 121574 146343 103046 161253

I want to access the numbers at the end of the string so intend to do a match like

/(\d+)/s

My issue is that the number of values at the end contain a variable number of strings.

What is the best way to format the regexp to be able to access each of those numbers individually? I'm a C++ developer and am just learning Perl, so am trying to find the cleanest Perl way to accomplish this.

Thanks for your help.

brian d foy
  • 129,424
  • 31
  • 207
  • 592
Superpolock
  • 3,515
  • 7
  • 30
  • 24
  • 1
    \d does not match [0-9] in Perl 5.8 and 5.10; it matches any UNICODE character that has the digit attribute (including "\x{1815}", MONGOLIAN DIGIT FIVE). If you mean [0-9] you must either use [0-9] or use the bytes pragma (but it turns all strings in 1 byte characters and is normally not what you want). – Chas. Owens May 07 '09 at 16:41

4 Answers4

6

Just use the /g flag to make the match operator perform a global match. In list context, the match operator returns all of the results as a list:

@result = $string =~ /(\d+)/g;

This works if there are no other numbers than the trailing ones.

brian d foy
  • 129,424
  • 31
  • 207
  • 592
Fritz G. Mehner
  • 16,550
  • 2
  • 34
  • 41
4

You can use the match operator in a list context with the global flag to get a list of all your parenthetical captures. Example:

@list = ($string =~ /(\d+)/g);

Your list should now have the all the digit groups in your string. See the documentation on the match operator for more info.

A. Levy
  • 29,056
  • 6
  • 39
  • 56
1

"In scalar context, each execution of m//g finds the next match, returning true if it matches, and false if there is no further match" --(From perldoc perlop)

So you should be able to make a global regex loop, like so:

while ($string =~ /(\d+)/g) {
    push @queuelist, $1;
}
Plutor
  • 2,867
  • 2
  • 25
  • 29
0

I'd do something like this:

my @numbers;
if (m/Messages pushed to the Order Book queues ([\d\s]+)/) {
   @numbers = split(/\s+/, $1);
}

No need to cram it into one regex.

bmdhacks
  • 15,841
  • 8
  • 34
  • 55