34702

Regular expressions and Chinese

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.

Recommend

  • Writing to Excel sheet based on image difference using matlab
  • allow UTF-8 encoded filenames on (file-)webserver?
  • MSSQL - JPA - Character encoding for Special characters - appending 'N' nativeQuery
  • Run-time Error 424 Object Required UserForm doesnt exist
  • Android Use Non-Gregorian Calendars
  • Input buffer flush
  • Special chars in Amazon S3 keys?
  • How do I recognize a line break with a switch case that evaluates a char in Java?
  • Does SmartGit support git-svn?
  • how to check the textarea content is blank using javascript?
  • Using HTML/CSS for UI in XNA?
  • custom string delimiters stringtemplate-4
  • Raphael.js function getBBox give back NAN/NAN/NAN in IE8
  • matching similar elements in between two lists
  • C function strchr - How to calculate the position of the character?
  • Responsive left sidebar open close
  • SIP API media codecs
  • Prevent page break in text block with iText, XMLWorker
  • IE10 strips out hashtag from the URL
  • Create a link to a web page that runs a Javascript function on the page
  • C++ friend class std::vector
  • Validate jQuery plugin, field not required
  • SyntaxError: (irb):26: both block arg and actual block given
  • NUnit 3.0 TestCase const custom object arguments
  • Plotting line graph with factors in R
  • Trying to get the char code of ENTER key
  • How to make JSON.NET deserialize to Microsoft Date Time?
  • Unable to install Git-core+svn by MacPorts
  • Django simple Captcha “No module named fields” error
  • preg_replace Double Spaces to tab (\\t) at the beginning of a line
  • Can you perform a UNION without a subquery in SQLAlchemy?
  • Could not find rake using whenever rails
  • FFmpeg Conversion Error
  • Email format validation in mvc3 view
  • C# - Is there a limit to the size of an httpWebRequest stream?
  • Resize panoramic image to fixed size
  • How to add date and time under each post in guestbook in google app engine
  • When should I choose bucket sort over other sorting algorithms?
  • coudnt use logback because of log4j
  • Can Visual Studio XAML designer handle font family names with spaces as a resource?