regular expressions -- very basic grouping question

T

Tom

[abc] - matches "a", "b", or "c"
[^abc] - matches anything except "a", "b", and "c"
(abc) - matches "abc", with back reference
??? - matches anything except "abc" literally?? What is the syntax for
this expression?

If this kind of non-matching grouping expression doesn't exist, what's
an alternate way to have the regexp engine (perl in my case), match
"anything but 'abc'"?

Thanks!
 
T

Tom

don't know why I wrote perl -- javascript obviously. working on too
many things at once!
 
R

Randy Webb

Tom said the following on 11/15/2006 2:21 PM:
[abc] - matches "a", "b", or "c"
[^abc] - matches anything except "a", "b", and "c"
(abc) - matches "abc", with back reference
??? - matches anything except "abc" literally?? What is the syntax for
this expression?

If this kind of non-matching grouping expression doesn't exist, what's
an alternate way to have the regexp engine (perl in my case), match
"anything but 'abc'"?

There is probably a RegEx that will do it, but, if all you want is a
match for abc then you can test with indexOf and if it is present or not
then act accordingly.
 
J

Julian Turner

Tom wrote:

[snip]
[abc] - matches "a", "b", or "c"
[^abc] - matches anything except "a", "b", and "c"
(abc) - matches "abc", with back reference
??? - matches anything except "abc" literally?? What is the syntax for
this expression?
[/snip]

Hi

Your question is ambiguous. Do you mean (a) you only want to accept a
string as a whole if it does not contain "abc", or (b) you only want
your pattern to accept an "a" if it is not followed by "bc", but it
does not matter if "abc" appears somewhere else?

If the answer is (a), then as already noted, you could use indexOf
instead.

If the answer is (b), then one way is to use a negative lookahead.

To quote from Microsoft's JScript documentation:-

(?!pattern)
"Negative lookahead matches the search string at any point where a
string not matching pattern begins. This is a non-capturing match, that
is, the match is not captured for possible later use. For example
'Windows (?!95|98|NT|2000)' matches "Windows" in "Windows 3.1" but does
not match "Windows" in "Windows 2000". Lookaheads do not consume
characters, that is, after a match occurs, the search for the next
match begins immediately following the last match, not after the
characters that comprised the lookahead."

Example:-

var re = /a(?!bc)/gi
var s = "adefgiabc";
alert(re.exec(s).length);

alerts 1, not 2.

Note that (?!pattern) is not supported by some older IE browsers.

Regards

Julian Turner
 
D

Dr J R Stockton

Wed said:
[abc] - matches "a", "b", or "c"
[^abc] - matches anything except "a", "b", and "c"
(abc) - matches "abc", with back reference
??? - matches anything except "abc" literally?? What is the syntax for
this expression?

If this kind of non-matching grouping expression doesn't exist, what's
an alternate way to have the regexp engine (perl in my case), match
"anything but 'abc'"?

I am using a standards-compliant newsreader. Your posting agent does
horrible things to your layout, as shown when viewing your article in my
reader; however, in reply mode I get the material quoted as presumably
intended. Please change your /modus operandi/ appropriately.

[abc] matches any character in the set "abc".
[^abc] matches any character not in the set "abc".

One approach to your problem, which should work even in older
RegExp-capable browsers, would be to use .replace(/abc/g, "\u0416"),
where \u0416 is a character which is known not to appear in the original
string, and you can then use [^\u0416] to be not matched.

It's a good idea to read the newsgroup and its FAQ. See below.
 
R

Randy Webb

Dr J R Stockton said the following on 11/16/2006 9:29 AM:
Wed said:
[abc] - matches "a", "b", or "c"
[^abc] - matches anything except "a", "b", and "c"
(abc) - matches "abc", with back reference
??? - matches anything except "abc" literally?? What is the syntax for
this expression?

If this kind of non-matching grouping expression doesn't exist, what's
an alternate way to have the regexp engine (perl in my case), match
"anything but 'abc'"?

I am using a standards-compliant newsreader. Your posting agent does
horrible things to your layout, as shown when viewing your article in my
reader; however, in reply mode I get the material quoted as presumably
intended. Please change your /modus operandi/ appropriately.

Sounds like it is something with your newsreader. I have read the
original using three different newsreaders and none of them screw up the
layout.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top