![]() |
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 |
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 |
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.