Question

Im using the following reg-ex which is working great but the only problem is that you cannot mix symbols like

aaa-bbbb\ccc

it always should have the same sperator

like aaa-bbb-cccc

"^(?:(?:-?[A-z0-9]+)*|(?:_?[A-z0-9]+)*|(?:\/?[A-z0-9]+/?)*)\s*$"

How can I change it ?

The Value should have

hyphen ‘-‘,
Underscore ‘_’
Slash ‘/’
Was it helpful?

Solution

First off, A-z gives you too wide of a range. In ASCII (and Unicode) there are characters between uppercase 'Z' and lowercase 'a' that are not letters or numbers. You can use the regex escape sequence \w for word characters or A-Za-z. Both are equivalent.

Also, it looks like you know you'll always have three sections so the lazy indicators are unnecessary.

[A-Za-z\d]+([-_\\/])[A-Za-z\d]+\1[A-Za-z\d]+\s*

This will ensure you have the same separator which can be a hyphen, slash, or underscore. Whatever the separator is it will separate 3 groups of alphanumeric characters.

Is this what you're looking for?

OTHER TIPS

To make sure the symbol is the same throughout you should use a back reference, e.g.

aaa[_/-]bbb\1ccc
           ^^

The \1 will have to be whatever symbol was matched in the [_/-]

Also you are using [A-z] which almost certainly doesn't do what you think it does, the characters between uppercase A and lowercase z are:

ABCD...XYZ[\]^_`abcd...xyz

You probably want [A-Za-z]

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top