Regular expression that accepts alphanumeric , non consecutive dash and non consecutive white space

后端 未结 4 1154
孤城傲影
孤城傲影 2020-12-10 23:33

Can anyone help me create a regular expression that accepts alphanumeric (numbers and letters only) and dashes and white spaces.

It shouldn\'t accept consecutive das

4条回答
  •  一向
    一向 (楼主)
    2020-12-10 23:55

    An ASCII answer

    ^(?!.*[- ]{2})(?!^[- ])(?!.*[- ]$)[A-Za-z0-9- ]+$
    

    See it here on Regexr

    ^ Matches the start of the string

    $ Matches the end of the string

    [A-Za-z0-9- ]+ Matches the characters you want, at least one

    The negative lookaheads

    (?!.*[- ]{2}) ensures that there are not - or space in a row

    (?!^[- ])(?!.*[- ]$) those two ensures that it does not start and not end with these characters.

    For an unicode answer you should specify the language you are using

    for some you can use \p{L} See here to describe a unicode code point that has the property "letter".

提交回复
热议问题