I'm having trouble wrapping my head around a regex.
so far my pattern looks like this ( Python Verbose flavor regex )
(?P<text>
[a-zA-Z0-9]+ # can start with "core char"
[a-zA-Z0-9\ \-]* # can have a "core char" or space|dash within it
[a-zA-Z0-9]+ # must end with a "core character"
)
I want to change this so within that middle section, I don't match on have a repeating space or dash. Having multiple spaces/dashes within the text is acceptable.
good:
hello world
hello-world
h-ll-w-rld
bad:
-hello-world
hello--world
h-ll--w-rld
hello world
Try this:
(?P<text>
[a-zA-Z0-9]+
([ -][a-zA-Z0-9]+)*
)
You can have something like the following:
^([a-zA-Z0-9]+[\ \-]?)*[a-zA-Z0-9]+$
http://rubular.com/r/VGfGTrqayR
If you ALWAYS want to have 2 or more words, than you'd use the following instead
^([a-zA-Z0-9]+[\ \-])+[a-zA-Z0-9]+$
http://rubular.com/r/EdV3iBQbsw
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With