Small confusion about negative lookbehind



I'm writing a small test program to illustrate several aspects of
regular expressions. In the section illustrating "lookaround"s, I
found something I didn't understand. My testing is with JDK 1.4.2.

My candidate string is "ab".

The expressions I'm testing this string against are the following,
which also lists whether the string matched or not

a(?=b) // succeeds
(?=a)b // fails
(?<=a)b // succeeds
a(?<=b) // fails
(?<!x)b // succeeds
a(?<!x) // succeeds(!)

Looking at these, I first wonder what exactly is the semantic
difference between a "lookbehind" and "lookahead" construct. The
syntactic difference is obvious, but I find the question of why pattern
1 succeeds and pattern 2 fails is a little hazy. The one that really
bothers me, however, is pattern 6. Despite the lack of clarity I have
in how this is supposed to work, I was pretty certain that this pattern
would fail.

I could use some clarification of these constructs.

.



Relevant Pages

  • [SUMMARY] Statistician I (#167)
    ... The heart of this problem, as suggested in the quiz description, is pattern ... have the pattern matching in place, the rest of the code is pretty trivial. ... use regular expressions, or at least review their knowledge. ... Next, as indicated in the comment, square brackets surrounding text are ...
    (comp.lang.ruby)
  • Re: position absolute different in IE than firefox, help!
    ... You referring to the F & R dialog box in BBEdit for Mac? ... Grep is the name of a frequently used Unix command that searches ... does is print out each line of the buffer that matches the pattern. ... The thing they have in common is regular expressions, ...
    (alt.html)
  • Re: Verifying email address format
    ... >>If you were to use Regular Expressions, ... I knew full-well that the Regular Expression pattern I suggested was flawed, ... I think the rules are fairly strictly as to what characters are ... While it's true that email addresses are irregular, ...
    (microsoft.public.access.formscoding)
  • Re: what is the quickest way to find out whether a string contains another string?
    ... Why would anyone want to use regular expressions to look for a plain ... Regular expressions are a tool for deciding whether a String or ... searching is a trivial degeneration of the problem addressed by regular ... input string into a pattern that matches only itself is the first task. ...
    (comp.lang.java.programmer)
  • Re: Split string on empty line
    ... not all UAs support the use of regular expressions as ... A safer pattern that allows for possible white space on the 'empty' line ...
    (comp.lang.javascript)