简体   繁体   English

NOTEPAD ++ REGEX-我无法获取两个字符串之间的内容,我无法获取

[英]NOTEPAD++ REGEX - I can't get what's in between two strings, I don't get it

I'm so close to understanding regex. 我非常了解正则表达式。 I'm a bit stumped, I thought i understood lazy and greedy. 我有些困惑,我以为我理解懒惰和贪婪。

Here is my current regex: <g_n><!\\[CDATA\\[([^]]+)(?=]]><\\/g_n>) 这是我当前的正则表达式: <g_n><!\\[CDATA\\[([^]]+)(?=]]><\\/g_n>)

My current regex makes: 我当前的正则表达式使:

<g_n><![CDATA[xxxxxxxxxx]]></g_n>

match to: 符合:

   <g_n><![CDATA[xxxxxxxxxx

But I want to make it match like this: 但是我想使它像这样匹配:

xxxxxxxxxx

You want 你要

<g_n><!\[CDATA\[(.*?)]]></g_n>

then if you want to replace it use 然后,如果您想更换它,请使用

\1 

in the replacement box 在替换框中

Your matching the whole string, the brackets around the .*? 您匹配整个字符串,。*周围的括号? match all of that and put it in the \\1 variable 匹配所有内容并将其放在\\ 1变量中

So the match will be all of the string with \\1 referring to what you want 因此,匹配将是所有带有\\ 1的字符串,表示您想要的内容

To change the xxxxx 更改xxxxx

Regex : 正则表达式:

(<g_n><![CDATA[)(?:.*?)(]]></g_n>) 

Replacement 替代

\1WHAT YOU WANT TO CHANGE TO\2

It looks like you need to add escape slashes to the two closing square brackets, as they are literals from the string you're parsing. 看起来您需要在两个闭合的方括号中添加转义斜杠,因为它们是您要分析的字符串中的文字。

<g_n><!\[CDATA\[.*+?\]\]><\/g_n>
                    ^ ^ 

Any square brackets not being escaped by backslashes will be treated as regex operational brackets, which in this case won't catch the input string. 任何没有被反斜杠转义的方括号将被视为正则表达式操作括号,在这种情况下,它将不会捕获输入字符串。

EDIT, I think the +? 编辑,我认为+? is redundant. 是多余的。

\[.*\]\]> ...

should suffice, since .* means any character, any amount of times. 应该足够,因为。*表示任何字符,任何时间。

Tested with notepad++ 6.3.2: 使用notepad ++ 6.3.2测试:

find: (<g_n><!\[CDATA\[)([^]]+)(?=]]></g_n>)
replace: $1WhatYouWant

You can replace + by * in the pattern to match void CDATA: 您可以在模式中用*替换+以匹配无效的CDATA:

<g_n><![CDATA[]]></g_n>

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

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