Connecting Tech Pros Worldwide Forums | Help | Site Map

regular expressions -- very basic grouping question

Tom
Guest
 
Posts: n/a
#1: Nov 15 '06
[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!


Tom
Guest
 
Posts: n/a
#2: Nov 15 '06

re: regular expressions -- very basic grouping question


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

Tom wrote:
Quote:
[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!
Randy Webb
Guest
 
Posts: n/a
#3: Nov 15 '06

re: regular expressions -- very basic grouping question


Tom said the following on 11/15/2006 2:21 PM:
Quote:
[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.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Julian Turner
Guest
 
Posts: n/a
#4: Nov 16 '06

re: regular expressions -- very basic grouping question



Tom wrote:

[snip]
Quote:
[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

Dr J R Stockton
Guest
 
Posts: n/a
#5: Nov 16 '06

re: regular expressions -- very basic grouping question


In message <1163618469.283689.22710@m7g2000cwm.googlegroups.c om>, Wed,
15 Nov 2006 11:21:09, Tom <biegel@gmail.comwrites
Quote:
>[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.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6
<URL:http://www.jibbering.com/faq/>? JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Randy Webb
Guest
 
Posts: n/a
#6: Nov 16 '06

re: regular expressions -- very basic grouping question


Dr J R Stockton said the following on 11/16/2006 9:29 AM:
Quote:
In message <1163618469.283689.22710@m7g2000cwm.googlegroups.c om>, Wed,
15 Nov 2006 11:21:09, Tom <biegel@gmail.comwrites
Quote:
>[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.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Closed Thread