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

  1. ^ matches the beginning of the string
  2. /.+ matches a string beginning by a “/” and following by at least one character
  3. | OR operator
  4. [^/].+ matches a string beginning by any character except “/” and following by at least one character
  5. $ 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.

Post a comment

Spam Protection by WP-SpamFree

Main Navigation

I am (not?) a Robot!

Recent Comments

Search

Subscribe and login