Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl Misc (http://www.velocityreviews.com/forums/f67-perl-misc.html)
-   -   Regexp problem (http://www.velocityreviews.com/forums/t881744-regexp-problem.html)

J Krugman 08-11-2003 01:08 AM

Regexp problem
 


Suppose I have some lengthy and/or complicated sub-regexp such as
'(foo|bar|...)', how do I write a regexp that will match it, followed
by some white space, followed by something that is neither foo or
bar? I.e. I want this to match:

bar frobozz

but not this:

bar foo

TIA,

Jill


Sam Holden 08-11-2003 01:26 AM

Re: Regexp problem
 
On Mon, 11 Aug 2003 01:08:37 +0000 (UTC),
J Krugman <jill_krugman@yahoo.com> wrote:
>
>
> Suppose I have some lengthy and/or complicated sub-regexp such as
> '(foo|bar|...)', how do I write a regexp that will match it, followed
> by some white space, followed by something that is neither foo or
> bar? I.e. I want this to match:


By translating your english into perl regular expression syntax:

match it: (foo|bar|...)

white space: \s+

something: \S

neither foo or bar : (?!foo|bar)

The neither bit if the only not so common construct, see "perldoc perlre"
for details.


And putting it all together:

/(foo|bar|...)\s+(?!foo|bar)\S/s


I made a bunch of assumptions about what your words meant, things like
"some" being 1 or more, "something" being 1 or more non-whitespace
characters. If they are wrong, then the regex will be wrong.

I also assumed you didn't want:

"foo barn"

to match, but did want:

"foo seafood"

to match. Again, if that's wrong the regex is wrong (but easily fixed).

--
Sam Holden


Benjamin Goldberg 08-12-2003 03:05 AM

Re: Regexp problem
 


J Krugman wrote:
>
> Suppose I have some lengthy and/or complicated sub-regexp such as
> '(foo|bar|...)', how do I write a regexp that will match it, followed
> by some white space, followed by something that is neither foo or
> bar? I.e. I want this to match:
>
> bar frobozz
>
> but not this:
>
> bar foo


my $first_part = qr/foo|bar|.../;

my $regex = qr/$first_part \s+ (?!$first_part) \S/x;

I'm not entirely sure how perl optomizes this, so you might want to
prevent perl from backtracking over the \s+, as follows:

my $regex = qr/$first_part (?>\s+) (?!$first_part) \S/x;

This should only have a significant effect if you've got a string such
as "bar foo" (with lots of whitespace in between the two
parts). Without the (?>) it might take an excessive time to fail.

--
$a=24;split//,240513;s/\B/ => /for@@=qw(ac ab bc ba cb ca
);{push(@b,$a),($a-=6)^=1 for 2..$a/6x--$|;print "$@[$a%6
]\n";((6<=($a-=6))?$a+=$_[$a%6]-$a%6:($a=pop @b))&&redo;}


All times are GMT. The time now is 01:54 AM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57