正则表达式 - 嵌套模式 - 在外部模式内但排除内部模式 [英] Regex - nested patterns - within outer pattern but exclude inner pattern

查看:9
本文介绍了正则表达式 - 嵌套模式 - 在外部模式内但排除内部模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含以下内容的文件.

I have a file with the content below.

<td> ${ dontReplaceMe } ReplaceMe ${dontReplaceMeEither} </td>

我想匹配 'ReplaceMe' 如果它在 td 标签中,但如果它在 ${ ... } 表达式中则不匹配.

I want to match 'ReplaceMe' if it is in the td tag, but NOT if it is in the ${ ... } expression.

我可以用正则表达式做到这一点吗?

Can I do this with regex?

目前拥有:

sed '/${.*?ReplaceMe.*?}/!s/ReplaceMe/REPLACED/g' data.txt

推荐答案

这是不可能的.

Regex 可用于Type-3 Chomsky 语言(常规语言).
但是,您的示例代码是Type-2 Chomsky 语言(无上下文语言).

Regex can be used for Type-3 Chomsky languages (regular language).
Your sample code however is a Type-2 Chomsky language (context-free language).

几乎只要涉及到任何类型的嵌套(括号),您就会处理上下文无关语言,而正则表达式未涵盖这些语言.

Pretty much as soon as any kind of nesting (brackets) is involved you're dealing with context free languages, which are not covered by regular expressions.

基本上没有办法在一对x和y中定义 在正则表达式中,因为这将要求正则表达式具有某种堆栈,而它没有(在功能上等同于有限状态自动机).

There is basically no way to define within a pair of x and y in a regular expression, as this would require the regular expression to have some kind of stack, which it doesn't (being functionally equivalent to a finite state automaton).

受到brandizzi 的挑战,寻找可能匹配至少微不足道的情况的正则表达式
我实际上想出了这个(痛苦地hacky)正则表达式模式:

Challenged by brandizzi to find a regex that might match at least trivial cases
I actually came up with this (painfully hacky) regex pattern:

perl -pe 's/(?<=<td>)((?:(?:{.*?})*[^{]*?)*)(ReplaceMe)(.*)(?=</td>)/$1REPLACED$3/g'

正确 (原文如此!) 匹配对于这些情况:

<td> ${ dontReplaceMe } ReplaceMe ${dontReplaceMeEither} </td>
<td> ReplaceMe ${dontReplaceMeEither} </td>
<td> ${ dontReplaceMe } ReplaceMe </td>
<td> ReplaceMe </td>

并且失败了 (嵌套是 Chomsky Type-2,还记得吗?;)):

<td>${ ${ dontReplaceMe } ReplaceMe ${dontReplaceMeEither} }</td>

而且它也不能替换多个匹配:

<td> ReplaceMe ReplaceMe </td>
<td> ReplaceMe ${dontReplaceMeEither} ReplaceMe </td>

获取领先的 $ 是一个棘手的部分.
这并保持 Reginald/Reggy 在编写这个野兽时不断崩溃.

Getting the leading $ covered was the tricky part.
This and keeping Reginald/Reggy from crashing constantly while writing this beast.

(……否则我会追捕你,如果我不得不使用你的代码/应用程序;)

这篇关于正则表达式 - 嵌套模式 - 在外部模式内但排除内部模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆