What regex can I use to match only letters, numbers, and one space between each word?

前端 未结 6 890
遇见更好的自我
遇见更好的自我 2021-02-01 05:01

How can I create a regex expression that will match only letters and numbers, and one space between each word?

Good Examples:



        
相关标签:
6条回答
  • 2021-02-01 05:20

    Most regex implementations support named character classes:

    ^[[:alnum:]]+( [[:alnum:]]+)*$
    

    You could be clever though a little less clear and simplify this to:

    ^([[:alnum:]]+ ?)*$
    

    FYI, the second one allows a spurious space character at the end of the string. If you don't want that stick with the first regex.

    Also as other posters said, if [[:alnum:]] doesn't work for you then you can use [A-Za-z0-9] instead.

    0 讨论(0)
  • 2021-02-01 05:20
    ([a-zA-Z0-9]+ ?)+?
    
    0 讨论(0)
  • 2021-02-01 05:25
    ^[a-zA-Z]+([\s][a-zA-Z]+)*$
    
    0 讨论(0)
  • 2021-02-01 05:26
    ^([a-zA-Z0-9]+\s?)*$
    

    its works

    0 讨论(0)
  • 2021-02-01 05:31
    (?:[a-zA-Z0-9]+[ ])+[a-zA-Z0-9]+
    

    If I understand you correctly the above regex should work. See screenshot below:

    screenshot http://img136.imageshack.us/img136/6871/screenshotkiki056.png

    0 讨论(0)
  • 2021-02-01 05:40

    This would match a word

    '[a-zA-Z0-9]+\ ?'
    
    0 讨论(0)
提交回复
热议问题