简体   繁体   English

Notepad++ 正则表达式匹配和替换某些标签之间的第一个逗号

[英]Notepad++ regex to match and replace first comma between certain tags

I have data like so opened in Notepad++:我有像这样在记事本++中打开的数据:

<title>Name 1, Address 1, NY</address>
<title>Name 2, Address 2, California</address>
<title>Name 3, Address 3, Texas</address>
<title>Name 4</title> <address>Address 4, Utah</address> <-- this line is 100% correct

...and I would llike to target the first comma on the groups that need proper tag enclosing, and replace it by: </title><address> ...并且我想将第一个逗号定位在需要适当标记封闭的组上,并将其替换为: </title><address>

I did this and it targets the second group (address), but the replace regex I don't know what to use to keep the integrity of the address data, I tried something like this but it destroys the whole address:我这样做了,它针对第二组(地址),但是替换正则表达式我不知道用什么来保持地址数据的完整性,我尝试了这样的事情,但它破坏了整个地址:

  • Find What: , (.+address)查找内容: , (.+address)
  • Replace with: </title><address>(.+address)替换为: </title><address>(.+address)

How to simply replace the first comma by new tags </title><address> ?如何用新标签</title><address>简单地替换第一个逗号?

You can use您可以使用

<title>[^<>,\v]*\K,\h*([^<>\v]*</address>)

Replace with </title> <address>\1 .替换为</title> <address>\1

See the regex demo .请参阅正则表达式演示

Details细节

  • <title> - a string <title> <title> - 一个字符串<title>
  • [^<>,\v]* - zero or more chars other than < , > , comma and any vertical whitespace [^<>,\v]* - 除了<> 、逗号和任何垂直空格之外的零个或多个字符
  • \K - match reset operator that disacards all text matched so far \K - 匹配重置运算符,丢弃到目前为止匹配的所有文本
  • , - a comma , - 逗号
  • \h* - zero or more horizontal whitespaces \h* - 零个或多个水平空格
  • ([^<>\v]*</address>) - Group 1 (the $1 or \1 backreference refers to the group value): ([^<>\v]*</address>) - 组 1( $1\1反向引用指的是组值):
    • [^<>\v]* - zero or more chars other than < , > and any vertical whitespace [^<>\v]* - 除了< , >和任何垂直空格之外的零个或多个字符
    • </address> - a </address> string. </address> - 一个</address>字符串。

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

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