Re: Small confusion about negative lookbehind



david.karr@xxxxxxxx wrote in message news:<1117493198.209099.208960@xxxxxxxxxxxxxxxxxxxxxxxxxxxx>...
> 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
a(?=b) There is a 'b' after me 'a' //succeeds, matches 'a' of "ab"
(?=a)b There is a 'a' of which prefix is 'b' //fails with "ab"
(?<=a)b There is a 'a' before a 'b' //succeeds, matches 'b' of "ab"
a(?<=b) There is a 'b' before a 'a' //fails with "ab"
(?<!x)b There is no 'x' before 'b' //succeeds, matches 'b' of "ab"
a(?<!x) There is no 'x' before 'a' //succeeds, matches 'a' of "ab"
> 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

  • 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: ACT scripting
    ... While executing the the script send by you..I am getting the following ... > 1) Assuming the pattern you are looking for is unique in the file, ... > 'string to search ... > Regular Expressions. ...
    (microsoft.public.scripting.vbscript)
  • Re: Pattern Match
    ... Is there any way to get them in one pattern? ... Here's a demo program I use to test out regular expressions. ... quotes if the expression has spaces) and a string to test (again ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: BInding operator fails
    ... you're looking for $rdns inside of $result. ... looking for any of the whitespace in your pattern. ... you have no reason to be using regular expressions. ... If you just want to see if one string is ...
    (perl.beginners)
  • Re: RegExp irregularity in JScript
    ... using Regular Expressions to do it. ... takes a pattern and an string, and tells us whether the string matches the ... pattern when the user clicks a button. ... But - when we validate some strings using this code, ...
    (microsoft.public.scripting.jscript)