$regex(*abd*,\*(?!abc)\*)
what's wrong? i'm trying to match inside the *'s something with 3 letters or less that is not equal to abc
The correct code would be
var %re = \*(?!abc)(.{0,3})\*
echo -a $regex(*abd*,%re)
Obviously you don't have to echo the result, but you do have to assign the regex pattern to a variable first since it contains a comma.
Your current pattern is saying:
- Match any string that contains a * that is not followed by 'abc', followed by another *. So in other words it will only match the string '**'.
What my pattern is saying is:
- Match any string that contains a * that is not followed by 'abc', followed by 0 to 3 characters, followed by another *.
thx
i'm probably don't know how the negative lookahead works
can u explain me ?
I think you were expecting the lookahead assertion to match the characters following '*' if they weren't 'abc'. That's not how it works. All it does is create a condition that must be met in order for the preceding part of the pattern (the '*') to be matched.
Your pattern "looked" at the string, saw that the '*' wasn't followed by 'abc', and so it matched the '*' and moved on to the next part of the pattern. The next part of the pattern said to match another '*', but in the string we're only up to the 'a' in 'abd', so it failed to match.
I doubt that helped you understand lookahead assertions, but that's the best that I can explain it.
Heh, assertions are probably the hardest thing to explain to somebody. They are not particularly complicated themselves and are extremely useful but you just can't explain them without getting the answer "huh??". I think the only way to really understand how they work is by oneself.
Assertions are things that you are testing the truth of. For example, the \w assertion checks the truth of "the next character is a word character" if it is, the assertion succeeds, if it's not, the assertion fails.
uh, so what is so hard about them ?
Well specifically look ahead/behind assertions are more confusing than that example. But I personally don't find assertions in general to be difficult. I find them to be very easy really, doing \W is much easier than doing [^[:word:]] for example.
Erm, \w or \W have nothing to do with assertions. The fundamental difference of assertions and stuff like [[:word:]], [^[:word:]], \w, \W etc is that the former are non-consuming items, while the latter are the opposite. \w "consumes" the part of the input it matches, so it cannot be used again for matching. Assertions, on the other hand, take a peek at the input string, either forward or backward (lookahead and lookbehind), and 'assert' the presence or absense (positive or negative) of certain features in the string, without "eating" those parts of the string themselves, so that regular (consuming) items can do that.