简体   繁体   English

C# Regex 选择带括号的预定义关键字

[英]C# Regex Select predefined keywords with bracket

I have a text which is我有一个文本是

SMA(20, V1) > 200000 AND C1 < LowerBollingerBand(20, 2)

after applying regex I need a result like this应用正则表达式后,我需要这样的结果

SMA(20, V1) LowerBollingerBand(20, 2)

I can select SMA and LowerBollingerBand by defining these keywords explicitly like this我可以通过像这样明确定义这些关键字来选择 SMA 和 LowerBollingerBand

(SMA|LowerBollingerBand)(\()

But I am unable to select whatever comes between brackets followed by brackets但我无法选择括号之间的任何内容,然后是括号

In case your strings cannot have nested balanced parentheses, you may use a [^()] negated character class to match any symbol but ( and ) after \\( and add a \\) after to match the close parentheses:如果您的字符串不能有嵌套的括号,你可以使用[^()]否定的字符类匹配任何符号,但()\\(和添加\\)之后,以配合关闭括号:

(SMA|LowerBollingerBand)\([^()]*\)

See the regex demo查看正则表达式演示

Details :详情

  • (SMA|LowerBollingerBand) - either SMA or LowerBollingerBand (SMA|LowerBollingerBand) - SMALowerBollingerBand
  • \\( - a ( \\( - 一个(
  • [^()]* - 0+ chars other than ( and ) [^()]* - 除()之外的 0+ 个字符
  • \\) - a ) . \\) - 一个)

In case there are balanced nested parentheses, use如果有平衡的嵌套括号,请使用

(SMA|LowerBollingerBand)\((?>[^()]+|(?<c>)\(|(?<-c>)\))*(?(c)(?!))\)

See this regex demo看到这个正则表达式演示

The \\((?>[^()]+|(?<c>)\\(|(?<-c>)\\))*(?(c)(?!))\\) construct matches balanced nested parentheses, see this answer for a description . \\((?>[^()]+|(?<c>)\\(|(?<-c>)\\))*(?(c)(?!))\\)结构匹配平衡的嵌套括号,请参阅此答案以获取说明 More on this can be found at the regular-expressions.info Balancing Groups .有关更多信息,请访问正则表达式.info平衡组

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

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