I’d like a regex (using Java) that captures three digits such as “876”, but not if they are buried inside a 4-digit sequence.
To capture “876” within “876” and “foo876″ and ” 876 ” and “876” and “food876” and “4foo876”.
But NOT within “88foo9876” or “9876” or “a8876” or “a8876foo”.
How do I do this?
I want to say something like X(\d\d\d)X, but in place of the first X in that to say “\D or ^ (start-string)” and in place of the second X in that to say “\D or $ (end-string)”.
Edit:
For answers, see Xanatos, also Code Jockey, and Tim Pietzcker.
well, then! for X(\d\d\d)X as you asked for, use
which is
and will
as you specified 😀
Here it is shown below in RegexBuddy:
if you’re using a language without lookbehind (like ECMA/JavaScript) you’ll have to either use