正则表达式替换多字节字符?

发布于 2024-10-09 05:49:58 字数 352 浏览 0 评论 0原文

是否有类似正则表达式的东西可以用它们自己替换多字节字符?

即我有一个很大的文本文件,其中包含这样的字符:

漁魚京供侠競共

并且想要将它们替换为:

漁;
魚;
京;
供;
...

如何使用正则表达式来做到这一点?我已经尝试过:

(.)

替换为

\1;\n

but 将多字节字符(即 utf8)“拆分”为每行一个字节(因此一个字符被拆分为 2 行以上)。对于单字节字符它工作得很好...任何帮助将不胜感激。

Is there something like an regular expression to replace multibyte characters with them self?

i.e. I have an large textfile with characters like this:

漁魚京供侠競共

and want to replace them like:

漁;
魚;
京;
供;
...

How can I do this, using a regular expression? I tried already:

(.)

replace with

\1;\n

but that "splits" multibyte characters (i.e. utf8) to one byte per line (so one character is over 2 lines splitted). For single-byte characters it works fine... Any help would be highly appreciated.

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

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

发布评论

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

评论(2

失去的东西太少 2024-10-16 05:49:58

我使用 Vim。我创建了一个新文件并将您的示例文本(渔鱼京供侠竞共)粘贴到一行中。然后我输入:

:%s/./\0;Ctrl+VEnter/g

这成功地分隔了行,因为您要求。

命令为:

  • : - 启动一个新命令(在 vim 命令行上)
  • % - 将更改应用于整个文件
  • s/// - 替换
  • \0 - 对整个原始匹配的反向引用(也可以使用 (.)\1
  • Ctrl +V - 将下一个击键转义为文字字符
  • g - 替换每行上的所有匹配项

I use Vim. I created a new file and pasted your sample text (漁魚京供侠競共) into a line. Then I typed:

:%s/./\0;Ctrl+VEnter/g

This successfully separates the lines as you require.

The commands are:

  • : - start a new command (on the vim command line)
  • % - apply the change to the whole file
  • s/// - substitute
  • \0 - a backreference to the whole original match (could have used (.) and \1 also)
  • Ctrl+V - escape the next keystroke as a literal character
  • g - replace all occurrences on each line
满地尘埃落定 2024-10-16 05:49:58

使用 \P{M}\p{M}* 作为替代。要匹配任意数量的字素,请使用 (?:\P{M}\p{M}*)+ 而不是 \X+

您可以使用

(?:\P{M}\p{M}*)

instead of

(.)

in .NET.

请参阅正则表达式 - Unicode 字符和属性

Use \P{M}\p{M}* as a substitute. To match any number of graphemes, use (?:\P{M}\p{M}*)+ instead of \X+

You can use

(?:\P{M}\p{M}*)

instead of

(.)

in .NET.

Refer Regex - Unicode Characters and properties

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