2
Text 1 : 
do not match: nw, n, s and somethingelse.
all directions are: n, w, s and e.
Expect :{n, w, s, e}


Text 2 : 
do not match: nw, n, s and somethingelse.
all directions are: nw, sw, se, w, ..., s and e.
Expect :{nw, sw, se, w, ..., s, e} 

Is it possible to capture all directions in one express?

Jun Zhou
  • 23
  • 5

3 Answers3

0

Are you looking for something like this?

my $text = "all directions are: nw, sw, se, w, ..., s and e.";

if( $text =~ /all directions are:\s+(([^,]+,\s+)+)(\w+)\s+and\s+(\w+)\./)
{
    print "$1 $3, $4\n";
}

Output: nw, sw, se, w, ..., s, e

Explanation:

([^,]+,\s+) matches a directory name followed by a comma (,) and some spaces. This can repeat n number of times in the string.

Then we have to match the x and y part. (\w+)\s+and\s+(\w+)\. will take care of that.

Thaha
  • 303
  • 3
  • 7
  • Question updated. some conditions missed, it's my bad, sorry. – Jun Zhou Dec 09 '15 at 07:44
  • Answer Updated. If you just give `all directions are` in the beginning, this will work, right? Or am I missing something here? – Thaha Dec 09 '15 at 08:14
0

It is not possible capture in one expression as explained here: Python regular expressions - how to capture multiple groups from a wildcard expression?

A soultion to your problem might be this:

use strict;
use warnings;
use Data::Dumper;

my $text = "all directions are: nw, sw, se, w, ..., s and e.";
my @capture;
if($text =~ s/all directions are: (\w+),\s+(.*)/$2/) {
    push @capture, $1;
    while($text =~ s/(\w+),\s+(.*)/$2/) {
        push @capture, $1;
    }
    if($text =~ /(\w+)\s+and\s+(\w+)\./) {
        push @capture, $1;
        push @capture, $2;
    }
}
print Dumper \@capture;
Community
  • 1
  • 1
Phyreprooph
  • 517
  • 2
  • 13
0

While, as Phyreprooph has explained, it's not possible with a single expression, you could use the /g ("global") modifier to match multiple times and yield a list of matches like this:

if(/all directions are:\s*(.*)/) {
    @dir = $1 =~ /(\.\.\.|\b[nsew]{1,2}\b)/g;
    print "{", join(", ", @dir), "}\n";
}
mbethke
  • 935
  • 8
  • 19