1

I need to pass strings like:

/test/

Some illegal things would be:

\test\
\test
/test
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
user2030856
  • 77
  • 1
  • 6
  • valid examples would be: C:/test/ ./test/ C:/Test/ it has to end with a forward slash and only contain forward slashes no backslashes at all... – user2030856 Mar 31 '14 at 18:07
  • found what the problem was: I must use 4 backslashes for it to work... ^[^\\\\]+$ – user2030856 Mar 31 '14 at 18:54

2 Answers2

1

Something like this should match your strings.

^/.*/$

So that regex expects a the string to start and end with a forward slash and can have anything in between.

xp500
  • 1,426
  • 7
  • 11
  • Note that `.*` would permit no characters to appear between the slashes. It would also allow one or more `/` characters to appear. Also, the `/` characters in the pattern may need to be escaped (depending on the language in which the regex is being interpreted). – DavidRR Mar 01 '14 at 02:49
  • @DavidRR He didn't say anything about what strings are valid. I agree with escaping the / (he didn't say in which context he's using them either, but you're right). – xp500 Mar 01 '14 at 02:51
  • Do you think that `/te/st/` should be allowed? My hunch given his examples is probably not. But yes, this is an educated guess. – DavidRR Mar 01 '14 at 02:54
  • 1
    @DavidRR I really can't tell, that's why I assumed anything is valid. Maybe he wants to have urls between slashes, I don't know...That's why I gave the minimum expression that matches what he asked. He can then replace the .* for whatever he needs. – xp500 Mar 01 '14 at 02:58
1

If you want to allow only lower-case alphabetic characters between the slash characters, try this:

^\/[a-z]+\/$

Explanation:

^      require match to start at the very beginning of the string
\      escape the forward slash in the input string
[a-z]  the character class representing the set of lower case characters
+      the preceding character or character class occurs one or more times
$      require match to end at the very end of the string

Edit: When I answered this question, I confess to probably not noticing the tag among the four tags originally on the question. Some regex parsers (such as that provided by Perl) require that a delimiter character be used to designate the start and end of a pattern. For such regex parsers, the forward slash / is commonly used as a delimiter character. If that is the case, it is necessary to escape a / that appears in the regex pattern.

A question has been raised if it is necessary to escape a / that appears in a regex pattern to be processed by qregexp. Perhaps not–I'll leave that to be answered by the qregexp experts. That said, for regexp parsers that do not required a / to be escaped, the escape character \ can be dropped from the pattern that I show above:

^/[a-z]+/$

Finally, if a particular regex might be used in more than one environment, it doesn't hurt to escape a character that might be deemed special in one of those environments.

Community
  • 1
  • 1
DavidRR
  • 18,291
  • 25
  • 109
  • 191