[英]RegEx find and replace with notepad++
I'm trying to find commas followed by space(s) and a string containing and underscore and replace with new line followed by my matched string. 我正在尝试查找逗号,后跟空格以及包含和下划线的字符串,并用新行替换后跟匹配的字符串。
Input 输入
ABC, ZYZ John_Doe
HBB Dan_Doe
HHH, BBB, CCC April_May
Desired output 所需的输出
ABC John_Doe
ZYZ John_Doe
HBB Dan_Doe
HHH April_May
BBB April_May
CCC April_May
I'm using Notepad++ and RegEx, I'm able to replace comma and space by putting , \\s+
in Find and \\n
in replace, but stuck on also matching a string containing underscore in that line and having it end up after new line. 我使用的是Notepad ++和RegEx,可以通过在查找中将
\\s+
和替换中的\\n
替换为逗号和空格,但也要匹配该行中包含下划线的字符串,并使其在换行后结束。
any help is much appreciated 任何帮助深表感谢
This regex will work when you have one comma 当您有一个逗号时,此正则表达式将起作用
Find: ^([AZ]{3}), ([AZ]{3}) ([AZ][az].*)
查找:
^([AZ]{3}), ([AZ]{3}) ([AZ][az].*)
Replace with: \\1 \\3\\n\\2 \\3
替换为:
\\1 \\3\\n\\2 \\3
This will work with two commas 这将使用两个逗号
Find: ^([AZ]{3}), ([AZ]{3}), ([AZ]{3}) ([AZ][az].*)
查找:
^([AZ]{3}), ([AZ]{3}), ([AZ]{3}) ([AZ][az].*)
Replace with: \\1 \\4\\n\\2 \\4\\n\\3 \\4
替换为:
\\1 \\4\\n\\2 \\4\\n\\3 \\4
Note sure if this is the best approach, but I came up with the following pattern: (?:(?:^|\\G(?!^)\\h*,\\h*)([[:alnum:]]+\\b)(?=(\\h*,\\h*[[:alnum:]]+)*\\h+([[:alnum:]]+_[[:alnum:]]+\\h*$))|(\\h+[[:alnum:]]+_[[:alnum:]]+\\h*$\\R?))
, replacing it with (?{1}$1 $3\\n(?{2}~~:):)
and a second replace with ^~~
and replacing with an empty string. 请注意,这是否是最好的方法,但是我想出了以下模式:
(?:(?:^|\\G(?!^)\\h*,\\h*)([[:alnum:]]+\\b)(?=(\\h*,\\h*[[:alnum:]]+)*\\h+([[:alnum:]]+_[[:alnum:]]+\\h*$))|(\\h+[[:alnum:]]+_[[:alnum:]]+\\h*$\\R?))
,将其替换为(?{1}$1 $3\\n(?{2}~~:):)
,第二个替换为^~~
并替换为空字符串。
It converts as you desired, working for an arbitrary number of commas. 它可以根据需要进行转换,可以处理任意数量的逗号。 Here's what it does:
这是它的作用:
(?:^|\G(?!^)\h*,\h*)([[:alnum:]]+\b)(?=(\h*,\h*[[:alnum:]]+)*\h+([[:alnum:]]+_[[:alnum:]]+)\h*$)
This matches the comma separated strings (and the underscored value in a lookahead) 这匹配以逗号分隔的字符串(以及前瞻中的下划线值)
(?:^|\\G(?!^)\\h*,\\h*)
matches the start of the line, or the previous match followed by horizontal spaces comma horizontal spaces (?:^|\\G(?!^)\\h*,\\h*)
匹配行的开头,或前一个匹配项后跟水平空格,用 逗号隔开 水平空格 ([[:alnum:]]+\\b)
matches letters/numbers followed by a word boundary, stores into capturing group 1 ([[:alnum:]]+\\b)
匹配字母/数字和单词边界,存储到捕获组1中 (?=
starts a lookahead, so we don't actually match, just assert and store into capturing groups (?=
开始前瞻,因此我们实际上并不匹配,只是断言并存储到捕获组中 (\\h*,\\h*[[:alnum:]]+)*
matches the next words, if there are any following and stores the last into capturing group 2, if there is no following word, capturing group 2 is not matched (\\h*,\\h*[[:alnum:]]+)*
匹配下一个单词,如果有后续单词,则将最后一个存储到捕获组2中,如果没有后续单词,则捕获组2不匹配 \\h+([[:alnum:]]+_[[:alnum:]]+)\\h*$
matches the underscored word, captures it into group 3, there are horizontal spaces before and might be after \\h+([[:alnum:]]+_[[:alnum:]]+)\\h*$
匹配带下划线的单词,将其捕获到第3组中,之前和之后可能有水平空格 (\\h+[[:alnum:]]+_[[:alnum:]]+\\h*$\\R?))
This matches the underscored word and optionally the following newline, so we can replace it with an empty string. 这与带下划线的单词以及以下可选的换行符匹配,因此我们可以将其替换为空字符串。
The replacement (?{1}$1 $3\\n(?{2}~~:):)
checks if the first capturing group is matched (thus one of the comma separated words). 替换
(?{1}$1 $3\\n(?{2}~~:):)
检查第一个捕获组是否匹配(因此用逗号分隔的单词之一)。 If so it inserts this word a space, the underscored word, a newline and ~~
if it's not the last one. 如果是这样,它将在此单词后面插入空格,下划线单词,换行符,如果不是最后一个单词,则插入
~~
。 I needed ~~
to make the \\G
work correctly in all cases, you could use any string that's unlikely to reappear in the content. 我需要
~~
以使\\G
在所有情况下都能正常工作,您可以使用不太可能在内容中再次出现的任何字符串。 If the first capturing group is not matched, the replacement will be empty. 如果第一个捕获组不匹配,则替换将为空。 The second replace with
^~~
is used to finally remove it. 第二次用
^~~
替换最终被删除。
Use the following regex N times, where N is the maximum count of commas , in the same line: 使用下面的正则表达式N次,其中N是逗号的最大数,在同一条直线:
, ([AZ]*)( \\w*)
Replace with: 用。。。来代替:
$2\\r\\n$1$2
Here is an useful site for testing. 这是一个有用的测试站点 。
Example: 例:
Starting from the input text you posted, you'll have to use the regex twice: 从您发布的输入文本开始,您将不得不使用两次正则表达式:
ABC, ZYZ John_Doe
HBB Dan_Doe
HHH, BBB, CCC April_May
This will be the result of the first execution: 这将是第一次执行的结果:
ABC John_Doe
ZYZ John_Doe
HBB Dan_Doe
HHH, BBB April_May
CCC April_May
And this will be the result of the second: 这将是第二个结果:
ABC John_Doe
ZYZ John_Doe
HBB Dan_Doe
HHH April_May
BBB April_May
CCC April_May
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.