本文介绍了普通的前pressions和中国的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据库查询,根据用户的输入,但是,如果用户输入的搜索应用程序的任何一个普通的字母会崩溃。但随着搜索还支持中国字我不能简单的块中的所有一个字符搜索。

我将如何写一个正前pression检查一个英文字符,但不包括一个中国字? ^(A-ZA-Z){1} $,^ \\ w {1} $做这些只包括英文字母?

我想使用常规的前pression这样的,但没有更好的办法将AP preciated:

 如果(input.matches(^(A-ZA-Z){1} $)
{
  //显示错误
}
其他
{
 //做查询
}


解决方案

您一定要首先解决的崩溃。英语和中国(中日韩)字符之间的区别,您可以使用字符类,如 \\ p {ASCII} \\ p {字母} 为ASCII和 \\ p {InCJKUnifiedIdeographs} 适用于CJK字符。

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
}
解决方案

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.

这篇关于普通的前pressions和中国的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-14 08:52