RegEx for no whitespace at the beginning and end

南楼画角 提交于 2019-11-27 18:24:23

问题


I want to design an expression for not allowing whitespace at the beginning and at the end of a string, but allowing in the middle of the string.

The regex I've tried is this:

\^[^\s][a-z\sA-Z\s0-9\s-()][^\s$]\

回答1:


This should work:

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

If you want to include character restrictions:

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

Explanation:

the starting ^ and ending $ denotes the string.

considering the first regex I gave, [^\s]+ means at least one not whitespace and \s+ means at least one white space. Note also that parentheses () groups together the second and third fragments and * at the end means zero or more of this group. So, if you take a look, the expression is: begins with at least one non whitespace and ends with any number of groups of at least one whitespace followed by at least one non whitespace.

For example if the input is 'A' then it matches, because it matches with the begins with at least one non whitespace condition. The input 'AA' matches for the same reason. The input 'A A' matches also because the first A matches for the at least one not whitespace condition, then the ' A' matches for the any number of groups of at least one whitespace followed by at least one non whitespace.

' A' does not match because the begins with at least one non whitespace condition is not satisfied. 'A ' does not matches because the ends with any number of groups of at least one whitespace followed by at least one non whitespace condition is not satisfied.

If you want to restrict which characters to accept at the beginning and end, see the second regex. I have allowed a-z, A-Z, 0-9 and () at beginning and end. Only these are allowed.

Regex playground: http://www.regexr.com/




回答2:


This RegEx will allow neither white-space at the beginning nor at the end of your string/word.

^[^\s].+[^\s]$

Any string that doesn't begin or end with a white-space will be matched.

Explanation:

  1. ^ denotes the beginning of the string.
  2. \s denotes white-spaces and so [^\s] denotes NOT white-space. You could alternatively use \S to denote the same.
  3. . denotes any character expect line break.
  4. + is a quantifier which denote - one or more times. That means, the character which + follows can be repeated on or more times.

You can use this as RegEx cheat sheet.




回答3:


if the string must be at least 1 character long, if newlines are allowed in the middle together with any other characters and the first+last character can really be anyhing except whitespace (including @#$!...), then you are looking for:

^\S$|^\S[\s\S]*\S$

explanation and unit tests: https://regex101.com/r/uT8zU0




回答4:


How about:

^\S.+\S$

This will match any string that doesn't begin or end with any kind of space.




回答5:


^[^\s].+[^\s]$

That's it!!!! it allows any string that contains any caracter (a part from \n) without whitespace at the beginning or end; in case you want \n in the middle there is an option s that you have to replace .+ by [.\n]+




回答6:


pattern="^[^\s]+[-a-zA-Z\s]+([-a-zA-Z]+)*$" This will help you accept only characters and wont allow spaces at the start nor whitespaces.




回答7:


^[^0-9 ]{1}([a-zA-Z]+\s{1})+[a-zA-Z]+$

-for No more than one whitespaces in between , No spaces in first and last.

^[^0-9 ]{1}([a-zA-Z ])+[a-zA-Z]+$

-for more than one whitespaces in between , No spaces in first and last.




回答8:


This is the regex for no white space at the begining nor at the end but only one between. Also works without a 3 character limit :

\^([^\s]*[A-Za-z0-9]\s{0,1})[^\s]*$\ - just remove {0,1} and add * in order to have limitless space between.




回答9:


As a modification of @Aprillion's answer, I prefer:

^\S$|^\S[ \S]*\S$
  • It will not match a space at the beginning, end, or both.
  • It matches any number of spaces between a non-whitespace character at the beginning and end of a string.
  • It also matches only a single non-whitespace character (unlike many of the answers here).
  • It will not match any newline (\n), \r, \t, \f, nor \v in the string (unlike Aprillion's answer). I realize this isn't explicit to the question, but it's a useful distinction.



回答10:


Other answers introduce a limit on the length of the match. This can be avoided using Negative lookaheads and lookbehinds:

^(?!\s)([a-zA-Z0-9\s])*?(?<!\s)$

This starts by checking that the first character is not whitespace ^(?!\s). It then captures the characters you want a-zA-Z0-9\s non greedily (*?), and ends by checking that the character before $ (end of string/line) is not \s.

Check that lookaheads/lookbehinds are supported in your platform/browser.




回答11:


Letters and numbers divided only by one space. Also, no spaces allowed at beginning and end.

/^[a-z0-9]+( [a-z0-9]+)*$/gi


来源:https://stackoverflow.com/questions/34974942/regex-for-no-whitespace-at-the-beginning-and-end

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!