开发者

regex to find start of word and next word

Need to find every instance of a phrase that has the word FOO and has BAR some time after but on the same line.

It is formatted开发者_StackOverflow社区 like: [asdf_123/FOO_sdfsdsadasff.x] BAR 123


A simple

FOO.*BAR

will probably do

Update after language turned out to be perl

#!/usr/bin/perl

while (<DATA>)  {
    print  if $_ =~ /FOO.*BAR/;
}

__DATA__
[asdf_123/FOO_sdfsdsadasff.x] BAR 123
illegal string
FOO without
without BAR

output:

[asdf_123/FOO_sdfsdsadasff.x] BAR 123


How about FOO.*BAR; does that work?


.*FOO.*BAR.*

should work fine.


The question is a little ambiguous about what constitutes a 'phrase'
but here could be one way to do it.

use strict;
use warnings;

my @samples = (
 'asdf_123/FOO_sdfsdsadasff.x BAR 123',
 'FOOBAR BARFOO /ssBARaa',
 'asdf FOOkkBAR zzFOO BAR',
);

for (@samples)
{
    my @phrases = / (\S*FOO\S*)\s (?=.*BAR) /xg;
    print "'$_'\n" for @phrases;
}

__END__

Output:

'asdf_123/FOO_sdfsdsadasff.x'
'FOOBAR'
'BARFOO'
'FOOkkBAR'
'zzFOO'

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜