简体   繁体   English

正则表达式:检查字符串是否包含任何给定字符

[英]Regular Expression: check if a string contains any given characters

I need to check if a string contains any of this characters: 我需要检查字符串是否包含以下任何字符:

Á,À,Ã,É,Ê,Í,Ó,Õ,Ô,Ú,Ç Á,À,Ã,É,Ê,Í,Ó,Õ,Ô,Ú,Ç

I was thinking of doing a 我当时想做一个

"blá".contains(regexExpression) “blá”。包含(regexExpression)

Am I thinking right? 我在想对吗? If so, how can I do it? 如果是这样,我该怎么办? I don't know how will be the regular Expression 我不知道正则表达式会如何

Take a look at regular-expressions.info . 看一下regular-expressions.info There you find a good reference on how you can achieve certain things using a regex. 在那里,您可以找到有关如何使用正则表达式实现某些目标的很好的参考。

Note that matches(regex) will only return true, if the whole string matches the regex. 请注意,如果整个字符串与regex匹配,则matchs matches(regex)将仅返回true。 If you just want to know if one of the specified characters is in the String, use this: 如果只想知道指定字符之一是否在字符串中,请使用以下命令:

String input = "blá";
input.toUpperCase().matches(".*[ÁÀÃÉÊÍÓÕÔÚÇ].*");

Edit: if you need to match more unicode characters, have a look at the regular-expressions.info unicode reference. 编辑:如果您需要匹配更多的unicode字符,请查看regular-expressions.info unicode参考。

Pattern regex = Pattern.compile("[ÁÀÃÉÊÍÓÕÔÚÇ]");
Matcher regexMatcher = regex.matcher(subjectString.toUpperCase());
if (regexMatcher.find()) {
    // Successful match
} else {
    // Match attempt failed
} 

I my experience, better don't use a character, but use a hex representation . 根据我的经验,最好不要使用字符,而应使用十六进制表示法

for example: 例如:

'Á' - 0x00C1
'á' - 0x00E1

hex code for an any symbol you can find here http://www.fileformat.info/info/unicode . 您可以在http://www.fileformat.info/info/unicode中找到任何符号的十六进制代码。 Just put letter to search field. 只需在搜索栏中输入字母即可。

Your regex will be: 您的正则表达式将是:

[\x{00c1}\x{00e1}]++

This will work in PHP. 这将在PHP中工作。 In Java will be \Á\á, if sure to www.regular-expressions.info 如果要访问www.regular-expressions.info ,则在Java中为\\ u00c1 \\ u00e1

Also you can use range: 您还可以使用范围:

ÀÁÂÃÄÅÆ will be [\u00c0-\u00c6]++

Latin Supplement 拉丁语补充

If you need to find an any symbol from a Latin-1 Supplement range, you can use the following re: 如果您需要从Latin-1补码范围中找到任何符号,则可以使用以下代码:

[\p{InLatin-1_Supplement}]++

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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