views:

30

answers:

1

I'm using the following regular expression in my code to validate option symbols:

^([A-Za-z]{1,5}\d? +\b(?<=^.{6}))[0-9]{6}[CcPp][0-9]{8}$

If you notice, I'm using a negative lookbehind assertion to confirm that the previous match was only 6 characters.

However, JScript Regex apparently doesn't support lookbehinds but it does support lookahead.

So, I thought that if I did something like:

^((?=^.{6})[A-Za-z]{1,5}\d? +\b)[0-9]{6}[CcPp][0-9]{8}$

This would work.

However, it appears to not be constraining it in the same way. In the first example, it was useful to say that "the match behind me is 6 characters long". However, now that it is a lookahead this isn't working because there will be 6 characters matched even if it isn't in that first group.

Some examples of test cases I'm working with:

Should fail:
1GOOG 12E456C12345678
 GOOG 12E456C12345678
GE 4  12E456C12345678
GE4    12E456C12345678

Should pass:
GOOG1 123456C12345678
F5    123456C12345678

How can I succesfully convert this lookbehind into a lookahead so that I can use this expression in javascript?

+2  A: 

Why not check for 5 characters, then a space, then a non-space?

(?=.{5}\s\S)

(or in the context of the full pattern...)

^(?=.{5}\s\S)([A-Za-z]{1,5}\d? +\b)[0-9]{6}[CcPp][0-9]{8}$
Amber
That will match `GE 4 123456C12345678`. A false positive. There are two spaces between the 4 and the 1. Comments truncate spaces!
Welbog
No it won't - because the actual pattern (*not* the lookahead) won't match it. There's only one place in the actual matching pattern that accepts whitespace.
Amber
Looks like it has to start with 1 to 5 characters followed by a number, so he cannot match against .
Brandon Horsley
Fair enough! Yeah, that looks like it'll work fine, then.
Welbog