开发者

Regex to capture words between a specific word

I'm trying to get a regex that matches: (It should not match any other string)

Word1 or Word2 or Word3 or Wordn

Capturing the words between before or after an "or"

1: Word1
2: Word2
3: Word3
n: Wordn
开发者_Python百科

I've tried modifying a csv regex:

(?:^|,)(\"(?:[^\"]+|\"\")*\"|[^,]*)

to

(?:^|(?:or)((?:[^(?:or)]+)*|[^(?:or)]*)

But that does not give me what I want.

I'm sure I'm missing something, but I've been banging my head for hours.


How about:

my $string = "  foo or    bar or  foobar ";
if ( $string =~ m|^\s*[^\s]+(\s+or\s+[^\s]+)+\s*$| ) {
    my $tmp = "$string";
    $tmp =~ s|^\s+||;
    $tmp =~ s|\s+$||;
    my @words = split( /\s+or\s+/, $tmp );
    printf( "Found %d words:\n", scalar( @words ) );
    foreach my $word ( @words ) {
        print( "\t'$word'\n" );
    }
} else {
    print( "No match\n" );
}

The above will output:

Found 3 words:
    'foo'
    'bar'
    'foobar'


Try splitting the string on ' or '.


You know, this isn't something for which I'd naturally reach for regex. I'd try a split first.

my @words = split / or /, $string;


This regex will match any string that has at least word1 or word2, and any number more or's after that. It must have no whitespace at the beginning or end of the string as well, but you can remove the ^ and $ if you want to search for a string of this form within a larger string

(?:^(\w+)(?=\s+or))|(?:\s+or\s+(\w+))+

RegexPal


The real solution is to split on ' or '. A regex solution is not so straight forward.

$sm =~ / or / and @between_or = $sm =~ /(?:^\s*|(?<= or ))(.+?)(?= or |\s*$)/sg;

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜