简体   繁体   English

重音字符的正则表达式

[英]REGEX for accented characters

I need to build a regex (in PHP) that match if a string has at least one character different from a set provided.如果字符串至少有一个与提供的集合不同的字符,我需要构建一个匹配的正则表达式(在 PHP 中)。 Here is my set:这是我的一套:

A to Z and a to z, numbers, and áéíóúüÁÉÍÓÚÜ¡¿?:-: A 到 Z 和 a 到 z、数字和 áéíóúüÁÉÍÓÚÜ¡¿?:-:

So this word should not match: Sebastián This match: Sebastiàn所以这个词不应该匹配:Sebastián 这个匹配:Sebastiàn

I reprhased the question due to the comment of varchar256.由于 varchar256 的评论,我重新提出了这个问题。 BTW, when examples differ from verbose instructions, take the example as the true intention of the author of the doc.顺便说一句,当示例与详细说明不同时,请将该示例作为文档作者的真实意图。

preg_match('/[^A-Za-z0-9áéíóúüÁÉÍÓÚÜ¡¿!?\-:]/', ...);

Note the ^ inside the brackets - that inverts the match, so this regex will match on any strings which contains at least one character that is NOT listed inside the brackets.请注意括号内的^ - 反转匹配,因此此正则表达式将匹配包含至少一个未在括号内列出的字符的任何字符串。

Use a character set pattern: preg_match('/[^\wáéíóúüÁÉÍÓÚÜ¡¿?:-:]/')使用字符集模式: preg_match('/[^\wáéíóúüÁÉÍÓÚÜ¡¿?:-:]/')

$string = "Sebastiàn";

$clean_string = preg_replace("/[^a-zA-ZáéíóúüÁÉÍÓÚÜ¡¿!?\\-:]*/",$string);

make sure to escape the dash in character class.确保在字符 class 中转义破折号。

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

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