
Question:
I have a database query based on user input, However if the user inputs any one common letter in the search the app will crash. but as the search also supports Chinese characters I can't simple block all one character searches.
How would I write a regular expression to check for a single English character but will not include a single Chinese characters? "^(a-zA-Z){1}$" "^\w{1}$" do these include only english letters??
I'm thinking of using the regular expression like this, but any better solutions would be appreciated:
if(input.matches("^(a-zA-Z){1}$")
{
//Show error
}
else
{
//do query
}
Answer1:You should definitely fix any crashes first. To distinguish between English and Chinese (CJK) characters, you can use character classes such as \p{ASCII}
, \p{Alpha}
for ASCII and \p{InCJKUnifiedIdeographs}
for CJK characters.