Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RegEx for no whitespace at the beginning and end

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$]\
like image 915
pratik Avatar asked Jan 24 '16 11:01

pratik


People also ask

How do I get rid of white space in regex?

The replaceAll() method accepts a string and a regular expression replaces the matched characters with the given string. To remove all the white spaces from an input string, invoke the replaceAll() method on it bypassing the above mentioned regular expression and an empty string as inputs.

What is a non-whitespace character in regex?

The \S metacharacter matches non-whitespace characters. Whitespace characters can be: A space character.

Which modifier ignores white space in regex?

Turn on free-spacing mode to ignore whitespace between regex tokens and allow # comments. Turn on free-spacing mode to ignore whitespace between regex tokens and allow # comments, both inside and outside character classes.

What does \d mean in regex?

In regex, the uppercase metacharacter is always the inverse of the lowercase counterpart. \d (digit) matches any single digit (same as [0-9] ). The uppercase counterpart \D (non-digit) matches any single character that is not a digit (same as [^0-9] ).


3 Answers

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/

like image 103
war1oc Avatar answered Oct 21 '22 16:10

war1oc


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.

like image 8
Madhav Datt Avatar answered Oct 21 '22 16:10

Madhav Datt


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

like image 6
Aprillion Avatar answered Oct 21 '22 15:10

Aprillion