如何将西里尔字母与正则表达式匹配

发布于 2024-08-11 08:17:03 字数 72 浏览 6 评论 0原文

如何将法语和俄语西里尔字母字符与正则表达式匹配?我只想处理字母字符,没有数字或特殊字符。现在我有

[A-Za-z]

How do I match French and Russian Cyrillic alphabet characters with a regular expression? I only want to do the alpha characters, no numbers or special characters. Right now I have

[A-Za-z]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

枕梦 2024-08-18 08:17:03

如果您的 regex 风格支持 Unicode 块 ([\p{IsCyrillic}]),则可以匹配 西里尔字母 字符与:

[\p{IsCyrillic}] or [\p{Cyrillic}]

否则尝试使用:

[U+0400–U+04FF]

对于 PHP 使用:

[\x{0400}-\x{04FF}]

解释:

[\p{IsCyrillic}]

Match a character from the Unicode block "Cyrillic" (U+0400–U+04FF) «[\p{IsCyrillic}]»

注意:

[U+0400–U+04FF]

If your regex flavor supports Unicode blocks ([\p{IsCyrillic}]), you can match Cyrillic characters with:

[\p{IsCyrillic}] or [\p{Cyrillic}]

Otherwise try using:

[U+0400–U+04FF]

For PHP use:

[\x{0400}-\x{04FF}]

Explanation:

[\p{IsCyrillic}]

Match a character from the Unicode block "Cyrillic" (U+0400–U+04FF) «[\p{IsCyrillic}]»

Note:

Unicode Characters list and Numeric HTML Entities of [U+0400–U+04FF] .

梦途 2024-08-18 08:17:03

这取决于您的正则表达式风格。如果它支持 Unicode 字符类(例如 .NET),则 \p{L} 匹配字母字符(任何字符集中)。

It depends on your regex flavor. If it supports Unicode character classes (like .NET, for instance), \p{L} matches a letter character (in any character set).

伴我老 2024-08-18 08:17:03

要仅匹配俄语西里尔字母,请使用:

[\u0401\u0451\u0410-\u044f]

相当于:

[ЁёА-я]

其中 А 是西里尔字母,而不是拉丁字母。 (尽管看起来相同,但它们具有不同的代码)

\p{IsCyrillic}\p{Cyrillic}[\u0400-\u04FF]其他人建议将匹配西里尔字母的所有变体,而不仅仅是俄语

To match only Russian Cyrillic characters use:

[\u0401\u0451\u0410-\u044f]

which is the equivalent of:

[ЁёА-я]

where А is Cyrillic, not Latin. (Despite looking the same they have different codes)

\p{IsCyrillic}, \p{Cyrillic}, [\u0400-\u04FF] which others suggested will match all variants of Cyrillic, not only Russian

如梦亦如幻 2024-08-18 08:17:03

如果您使用现代 PHP 版本 - 只是:

preg_match("/^[\p{L}]+$/u");

不要忘记 unicode 支持的 u 标志!

If you use modern PHP version - just:

preg_match("/^[\p{L}]+$/u");

Don't forget the u flag for unicode support!

我做我的改变 2024-08-18 08:17:03

将西里尔字母与普通(英语)字母匹配的正则表达式:

^[A-Za-z.!@?#"$%&:;() *\+,\/;\-=[\\\]\^_{|}<>\u0400-\u04FF]*$

它匹配特殊字符、西里尔字母、英语字母。

Regex to match cyrillic alphabets with normal(english) alphabets :

^[A-Za-z.!@?#"$%&:;() *\+,\/;\-=[\\\]\^_{|}<>\u0400-\u04FF]*$

It matches special chars,cyrillic alphabets,english alphabets.

小嗲 2024-08-18 08:17:03

各种正则表达式方言使用 [:alpha:] 表示当前区域设置中的任何字母数字字符。 (您可能需要将其放入字符类中,例如 [[:alpha:]]。)

Various regex dialects use [:alpha:] for any alphanumeric character in the current locale. (You may need to put that in a character class, e.g. [[:alpha:]].)

日久见人心 2024-08-18 08:17:03

这对我有用

[a-z\u0400-\u04FF]

this worked for me

[a-z\u0400-\u04FF]
蹲在坟头点根烟 2024-08-18 08:17:03

您可以使用第一个和最后一个字母。例如保加利亚语:

[А-я]+

You can use the first and the last letter. For example in Bulgarian:

[А-я]+
哆兒滾 2024-08-18 08:17:03

如果您使用 Elixir:

String.match?(string, ~r/^\p{Cyrillic}*$/u)

您需要添加 u 标志以支持 unicode。

If you use Elixir:

String.match?(string, ~r/^\p{Cyrillic}*$/u)

You need to add the u flag for unicode support.

陌路黄昏 2024-08-18 08:17:03

对于现代 PHP(来源):

$string = 'тест тест Тест Обязателльно Stackoverflow >!<';
var_dump(preg_replace('/[\x{0410}-\x{042F}]+.*[\x{0410}-\x{042F}]+/iu', '', $string));

For modern PHP (source):

$string = 'тест тест Тест Обязателльно Stackoverflow >!<';
var_dump(preg_replace('/[\x{0410}-\x{042F}]+.*[\x{0410}-\x{042F}]+/iu', '', $string));
萌︼了一个春 2024-08-18 08:17:03

在 Java 中,要匹配西里尔字母和空格,请使用以下模式

^[\p{InCyrillic}\s]+$

In Java to match Cyrillic letters and space use the following pattern

^[\p{InCyrillic}\s]+$
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文