问题描述:

I am new at programming Java and using Regular Expressions.

I am trying to learn more about regular expressions, and I have the following question.

I want to create a regex that will:

  • match at least 2 characters
  • match multiple words
  • match any english characters

I have tried the following two regexes:

[a-zA-Z\\s{2,]]*|[a-zA-Z{2,}]

and

[a-zA-Z\\s]*|[a-zA-Z].{2,}

But they do not work. Can anyone help me understand how to write this regex?

网友答案:

Consolidated Restrictions:
Match strings consisting of at least 1 length 2+ character sequence restricted to the english characters (not sure if this is exactly what you want, but hopefully it will suffice):

regex = "^(?:\s*[a-zA-Z]{2,}\s*)+$"

"^": must begin with
"(?:...)": group, but do not capture
"\s": whitespace
"*": 0 or more
"[...]": character class
"[a-zA-Z]": restricts characters to lower/uppercase a,b,...,y,z
"{2,": 2 or more of the proceeding
"+": 1 or more
"$": must end here

相关阅读:
Top