Regular expression for no white space at start or end, but allow white space in middle, but also allow only one char inputs:

后端 未结 2 1406
耶瑟儿~
耶瑟儿~ 2020-12-10 09:31

Regular expression for no white space at start or end, but allow white space in middle, but also allow only one char inputs:

The closest I have got is : ^([^\\

相关标签:
2条回答
  • 2020-12-10 10:08

    Make everything after the first non-space character optional:

    ^(?=\S)[a-zA-Z0-9_\s-]*\S$
    
    0 讨论(0)
  • 2020-12-10 10:11

    Non-spaces, optionally followed by multiple groups of spaces then non-spaces:

    ^[^\s]+(\s+[^\s]+)*$
    

    Edit:

    To include the character restrictions, just replace non-space classes with allowable characters:

    ^[-_a-zA-Z0-9]+(\s+[-_a-zA-Z0-9]+)*$
    

    Also, consider being Unicode friendly and using something like

    ^[-_\p{Alnum}]+(\s+[-_\p{Alnum}]+)*$
    

    (assuming your regex interpreter allows it).

    0 讨论(0)
提交回复
热议问题