简体   繁体   中英

Regex to accept only alphabets and spaces and disallowing spaces at the beginning and the end of the string

I have the following requirements for validating an input field:

  1. It should only contain alphabets and spaces between the alphabets.
  2. It cannot contain spaces at the beginning or end of the string.
  3. It cannot contain any other special character.

I am using following regex for this:

^(?!\s*$)[-a-zA-Z ]*$

But this is allowing spaces at the beginning. Any help is appreciated.

This should work if you use it with String.matches method. I assume you want English alphabet.

"[a-zA-Z]+(\\s+[a-zA-Z]+)*"

Note that \\s will allow all kinds of whitespace characters. In Java, it would be equivalent to

[ \t\n\x0B\f\r]

Which includes horizontal tab (09), line feed (10), carriage return (13), form feed (12), backspace (08), space (32).

If you want to specifically allow only space (32):

"[a-zA-Z]+( +[a-zA-Z]+)*"

You can further optimize the regex above by making the capturing group ( +[a-zA-Z]+) non-capturing (with String.matches you are not going to be able to get the words individually anyway). It is also possible to change the quantifiers to make them possessive , since there is no point in backtracking here.

"[a-zA-Z]++(?: ++[a-zA-Z]++)*+"

For me the only logical way to do this is:

^\p{L}+(?: \p{L}+)*$

At the start of the string there must be at least one letter. (I replaced your [a-zA-Z] by the Unicode code property for letters \\p{L} ). Then there can be a space followed by at least one letter, this part can be repeated.

\\p{L} : any kind of letter from any language. See regular-expressions.info

The problem in your expression ^(?!\\s*$) is, that lookahead will fail, if there is only whitespace till the end of the string . If you want to disallow leading whitespace, just remove the end of string anchor inside the lookahead ==> ^(?!\\s)[-a-zA-Z ]*$ . But this still allows the string to end with whitespace. To avoid this look back at the end of the string ^(?!\\s)[-a-zA-Z ]*(?<!\\s)$ . But I think for this task a look around is not needed.

Try this:

^(((?<!^)\s(?!$)|[-a-zA-Z])*)$

This expression uses negative lookahead and negative lookbehind to disallow spaces at the beginning or at the end of the string, and requiring the match of the entire string.

I think the problem is there's a ? before the negation of white spaces, which means it is optional

This should work:

[a-zA-Z]{1}([a-zA-Z\s]*[a-zA-Z]{1})?

at least one sequence of letters, then optional string with spaces but always ends with letters

I don't know if words in your accepted string can be seperated by more then one space. If they can:

^[a-zA-Z]+(( )+[a-zA-z]+)*$

If can't:

^[a-zA-Z]+( [a-zA-z]+)*$

String must start with letter (or few letters), not space.

String can contain few words, but every word beside first must have space before it.

Hope I helped.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM