unicode 中的正则表达式断字器

发布于 2024-08-12 13:39:29 字数 69 浏览 5 评论 0原文

如何转换正则表达式 \w+ 给我 Unicode 中的整个单词——而不仅仅是 ASCII?

我使用.net

How do I convert the regular expression
\w+
To give me the whole words in Unicode – not just ASCII?

I use .net

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

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

发布评论

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

评论(4

习惯成性 2024-08-19 13:39:29

在 .NET 中,\w 将匹配 Unicode 字母或数字的 Unicode 字符。例如,它将匹配 ìÆ

要仅匹配 ASCII 字符,您可以使用 [a-zA-Z0-9]

In .NET, \w will match Unicode characters that are Unicode letters or digits. For example, it would match ì and Æ.

To just match ASCII characters, you could use [a-zA-Z0-9].

抚笙 2024-08-19 13:39:29

这对我来说按预期工作

        string foo = "Hola, la niña está gritando en alemán: Maüschen raus!";
        Regex r = new Regex(@"\w+");
        MatchCollection mc = r.Matches(foo);
        foreach (Match ma in mc)
        {
            Console.WriteLine(ma.Value);
        }

它输出

Hola
la
niña
está
gritando
en
alemán
Maüschen
raus

您使用 .Match() 而不是 .Matches()?

另一种可能的解释是,您期望收到的内容中有一个非单词字符,例如逗号。

This works as expected for me

        string foo = "Hola, la niña está gritando en alemán: Maüschen raus!";
        Regex r = new Regex(@"\w+");
        MatchCollection mc = r.Matches(foo);
        foreach (Match ma in mc)
        {
            Console.WriteLine(ma.Value);
        }

It outputs

Hola
la
niña
está
gritando
en
alemán
Maüschen
raus

Are you using .Match() instead of .Matches()?

Another possible explanation is that you have a non word character in what you expect to receive, like a comma.

我不在是我 2024-08-19 13:39:29

您应该查看 http://msdn.microsoft.com/ en-us/library/yd1hzczs.aspx#ECMAScript
还有一个在 .net 中使用正则表达式的不错的备忘单: http://regexlib.com/CheatSheet.aspx

You should take a look at http://msdn.microsoft.com/en-us/library/yd1hzczs.aspx#ECMAScript
There's also a nice Cheat Sheet for using regex in .net: http://regexlib.com/CheatSheet.aspx

妥活 2024-08-19 13:39:29

字母的“官方”Unicode 标识符是 \p{L},数字的“官方”Unicode 标识符是 \p{N}。因此,为了完整起见,在 \w 未扩展到 Unicode 字母/数字的情况下, \w+ 的等效项将是 [\p{L }\p{N}\p{Pc}]+。不要忘记下划线和其他“标点连接符”字符也包含在 \w 中(因此您可以自己决定是否保留它们)。

The "official" Unicode identifier for letters is \p{L}, for numbers \p{N}. So for completeness' sake, in cases where \w doesn't extend to Unicode letters/numbers, the equivalent for \w+ would be [\p{L}\p{N}\p{Pc}]+. Don't forget that the underscore and other "punctuation connector" characters are also contained in \w (so you can decide yourself whether to keep them or not).

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