简体   繁体   English

正则表达式与嵌套的Parens匹配

[英]Regex Match with nested Parens

OK, 好,

I don't know if it is possible to write this regex, so I am going to start by askign for help - so far I have had no success. 我不知道是否可以编写此正则表达式,所以我将首先寻求帮助-到目前为止,我还没有成功。

source string: 源字符串:

,convert(varchar(8000), lt.text) as reason

desired match: 所需的匹配项:

convert(varchar(8000), lt.text)

assumptions 假设

  • statement starts with a comma (outside of the parens) 语句以逗号开头(在括号之外)
  • state will end with "as " 状态将以“ as”结尾

this is the regex i am starting with: 这是我开始的正则表达式:

\(.+\)

Thank you 谢谢

Bar Kiers - is correct, the inability to cope "with an arbitrary amount of nesting", is the root cause of both my memory as to why and the reason as to why I couldn't accomplish what I wanted. Bar Kiers-是正确的,无法应对“任意数量的嵌套”,这是我对为什么做以及为什么我无法完成自己想要的事情的记忆的根本原因。

HACK ALERT: I solved by grabbing the inner most instance and replacing it out with a placeholder, and then processing recursively until there were no more matches... HACK ALERT:我通过抓住最里面的实例并用一个占位符替换掉它,然后递归处理直到没有更多匹配,解决了...

Use following regex : 使用以下正则表达式:

^\\,(?<imp>[\\s\\S]*?)as ^ \\,(?<小鬼> [\\ S \\ S] *?)作为

^,(.+\)) as [a-z]+$

I'm including the "reason" with [az]+. 我在[az] +中加入了“原因”。

I suggest you use the RightToLeft option, if you have it. 我建议您使用RightToLeft选项(如果有)。 It'll speed up the regex. 它将加快正则表达式的速度。 And remember to escape the \\ , if your language require it. 如果您的语言要求,请记住不要使用\\

I'm assuming PCRE syntax here. 我在这里假设PCRE语法。 If you can guarantee that the characters " as " don't show up within the parens, something like this should work: 如果您可以保证字符“ as”不会出现在括号中,则应执行以下操作:

{,(.*) as }

If you can't guarantee that " as " won't show up in the parens, you need to define some other conditions that will be met. 如果不能保证“ as”不会出现在括号中,则需要定义一些其他要满足的条件。 For example, if the line always ends after the " as ", something like this would work: 例如,如果该行始终在“ as”之后结束,则可以执行以下操作:

{,(.*) as \S+$}

Or you could use a real parser, which can keep context (the number of parens). 或者,您可以使用一个真正的解析器,该解析器可以保留上下文(parens的数量)。

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

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