RoyHB
RoyHB

Reputation: 1725

Add front slash to python regex

Currently, in a Python program, I use

self.regex_string = re.compile("^\w+(-\w+)*$")

To check an input string for alphanumeric plus hyphen characters.

I need to change the regex to allow front slashes ( / )

I'm lost in the forest of regexing. Could someone suggest a change to my regex that will add front slash to the allowable characters?

Alternatively, is there a more efficient regex that would allow ( A-z, a-z, 0-9, - and / ) with no spaces?

Thanks

Upvotes: 0

Views: 126

Answers (2)

wberry
wberry

Reputation: 19347

A narrow change to your regex so that in each place \w is allowed, / is also allowed:

^(\w|/)+(-(\w|/)+)*$

If you were using substitution groups, the group numbers change with this regex due to the additional parens. In Python source you can use a raw string to avoid escaping the \s most of the time:

self.regex_string = re.compile(r'^(\w|/)+(-(\w|/)+)*$')

Bonus, additionally rejecting strings containing // as requested:

self.regex_string = re.compile(r'^(\w|/(?!/))+(-(\w|/(?!/))+)*$')

This uses a zero-width negative lookahead assertion /(?!/) meaning "slash not followed by another slash".

Upvotes: 1

tckmn
tckmn

Reputation: 59273

You could try using a character class:

self.regex_string = re.compile("^\w+[\w\-/]*$")

Brackets ([]) define character classes, which will match anything inside of them. For example, [A-Cd-fG] would only allow A, B, C, d, e, f, and G.

Upvotes: 0

Related Questions