Regex Time, part 1
Oct 10, 2009
How to retrieve any string (of at least one character) except the one character string “/” (slash) with a regular expression?
Test Cases
These strings should not match our pattern:
- /
- an empty string
All other strings should match our pattern.
The Regular Expression Pattern
^/.+|[^/].+$
The explanation
^matches the beginning of the string/.+matches a string beginning by a “/” and following by at least one character|OR operator[^/].+matches a string beginning by any character except “/” and following by at least one character$matches the end of the string
So, this regular expression could be read as:
Match a string beginning by a “/” and following by at least one character or match a string beginning by any character except “/” and following by at least one character.