js******@sghms.ac.uk (Judas) writes:
am really confused by your question. As far as I can see your
first regular expression is eqivalent to
"/({0,1}:x)/"
As the {0,1}, or the ? don't actually follow anything, I'd be
surprised if it was a valid regular expression, and if it was,
something very funny would be going on.
Since you can't write ? (or {0,1}) without a token before, the syntax
"(?" was preciously illegal. That made it a prime candidate use for
when new features were needed.
In new and improved regular expressions, the sequence "(?" starts one of
the new features.
(?: ... ) - is a non-capturing grouping, just as ( ... ) but without
counting as a match
(?= ... ) - is a positive lookeahead. It has zero width but matches
if the following is matched by the expression inside.
(?! ... ) - is the negative lookeahed.
Example:
/^(?=\d{10}$)0*1*$/
This matches a sequence of ten 0's and 1's where all the zeros comes
before the ones. The (?=\d{10}$) matches a zero-width string where
the remainder of the string consists of 10 digits. The 0*1* matches
a string of any length of 0's and 1's where all the 0's comes before
all the 1's.
Example:
/^(?!\d{10}).{10}$/
This matches a string of ten characters that are not all digits.
The (?!\d{10}) matches a zero-width string that is not followed
by ten digit. The .{10} matches any ten characters (except newline).
So, a string is matched only if it is ten characters, and just
before it, the lookahead is not ten digits.
Both examples are of tests that are longer if you need to do them
without lookahead (at least I couldn't find a shorter way, and I'm
pretty good with regular expressions :).
/L
--
Lasse Reichstein Nielsen -
lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'