Regex Time, part 2
Oct 19, 2009
Today, one of my co-worker asked me to write a Regex. I love Regex, so it was a pleasure for me to answer him.
So, my co-worker wanted to know how to retrieve all digits (and only digits: 0 to 9) following a non-digit character and preceding a pipe “|” character ?
Test Cases
This kind of string should match our pattern:
- test123|another456|more789| (should match three strings: “123″, “456″ and “789″)
- justone1|
All other strings should not match our pattern.
The Regular Expression Pattern
(?<!\d)(\d+)(?=\|)
The explanation
(?<!\d): negative lookbehind expression tells to the regex engine that the character preceding our matching pattern must not be a digit. Because I’m using a lookaround which is a “zero-width assertion”, this first expression will not be a part of the matching string(\d+)matches one or more digit character. This group is the only capturing group (our matching string) of the regex.(?=|): positive lookahead expression tells to the regex engine that the character following our matching pattern must not be a pipe “|”. Because I’m using a lookaround which is a “zero-width assertion”, this third expression will not beĀ a part of the matching string
So, this regular expression could be read as:
Match a string of one or more digit character which is preceded by any character except a digit and which is followed by a pipe “|”.