REGEXP常规/递归查找/替换在记事本++

发布于 2025-01-24 18:18:23 字数 836 浏览 0 评论 0原文

如何将一些特定格式定义的字符串分开:

[length namevalue field]name=value[length namevalue field]name=value[length namevalue field]name=value[length namevalue field]name=value

在记事本++中的查找/替换时,二对名称=用白空间替换[长度名称value字段]的find/repens -regex? 主要问题与简单\ d {4}搜索不起作用的数字值有关。


例如。

输入:

0010name=mario0013surname=rossi0006age=180006phone=0014address=street
0013name=marianna0013surname=rossi0006age=210006phone=0015address=street1
0003name=pia0015surname=rossini0005age=30017phone=+39221122330020address=streetstreet

输出:

name=mario surname=rossi age=18 phone= address=street
name=mario surname=rossi age=18 phone= address=street
name=marianna surname=rossi age=21 phone= address=street1
name=pia surname=rossini age=3 phone=+3922112233 address=streetstreet

How to split some strings defined in a specific format:

[length namevalue field]name=value[length namevalue field]name=value[length namevalue field]name=value[length namevalue field]name=value

Is it possible with a Find/Replace regex in Notepad++ isolate the pair name=value replacing [length namevalue field] with a white space?
The main problem is related to numeric value where a simple \d{4} search doesn't work.


Eg.

INPUT:

0010name=mario0013surname=rossi0006age=180006phone=0014address=street
0013name=marianna0013surname=rossi0006age=210006phone=0015address=street1
0003name=pia0015surname=rossini0005age=30017phone=+39221122330020address=streetstreet

OUTPUT:

name=mario surname=rossi age=18 phone= address=street
name=mario surname=rossi age=18 phone= address=street
name=marianna surname=rossi age=21 phone= address=street1
name=pia surname=rossini age=3 phone=+3922112233 address=streetstreet

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

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

发布评论

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

评论(1

梦里人 2025-01-31 18:18:24

您可以使用

\d{4}(?=[[:alpha:]]\w*=)
\d{4}(?=[^\W\d]\w*=)

regex demo

模式匹配

  • \ d {4} - 四位数
  • (?= [:alpha:]] \ w*=)\ w*=) - ,它们紧随其后的是一封信,然后任何零或多个字char随后紧随当前位置右侧的= char。
  • (?= [^\ w \ d] \ w*=) - 紧随其后的是字母或下划线,然后任何零或更多单词chars随后遵循= CHAR立即位于当前位置的右侧。

在记事本++中,如果要在行开始时删除匹配项,并用其他任何地方替换为空间,则可以用

^(\d{4}(?=[[:alpha:]]\w*=))|(?1)

(?1:)替换。上面解释的模式,\ d {4}(?= [[:alpha:]] \ w*=),匹配, 捕获在第1组中,在行的开头(^),并且只是在其他任何地方匹配((?1)递归组1模式,以免重复它)。 (?1:)替换意味着如果组1匹配,我们用空字符串替换,否则,我们替换为空间。

请参阅演示屏幕截图:

“在此处输入图像描述”

You can use

\d{4}(?=[[:alpha:]]\w*=)
\d{4}(?=[^\W\d]\w*=)

See the regex demo.

The patterns match

  • \d{4} - four digits
  • (?=[[:alpha:]]\w*=) - that are immediately followed with a letter and then any zero or more word chars followed with a = char immediately to the right of the current position.
  • (?=[^\W\d]\w*=) - that are immediately followed with a letter or an underscore and then any zero or more word chars followed with a = char immediately to the right of the current position.

In Notepad++, if you want to remove the match at the start of the line and replace with space anywhere else, you can use

^(\d{4}(?=[[:alpha:]]\w*=))|(?1)

and replace with (?1: ). The above explained pattern, \d{4}(?=[[:alpha:]]\w*=), is matched and captured into Group 1 if it is at the start of a line (^), and just matched anywhere else ((?1) recurses the Group 1 pattern, so as not to repeat it). The (?1: ) replacement means we replace with empty string if Group 1 matched, else, we replace with a space.

See the demo screenshot:

enter image description here

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