简体   繁体   English

带点括号,正则表达式

[英]Brackets with dot, regular expressions

I want delete brakets that ends on a dot from string. 我想删除以字符串中的点结尾的刹车。 I use regular expresion - @"\\([^)]+\\)\\." 我使用常规表达式- @"\\([^)]+\\)\\." it works with string like this - some text (some text) some (text). 它适用于这样的字符串- some text (some text) some (text). , after regular expression I have string - some text (some text) some But this not work with string like that - some text (some text) some (text (text) some). ,在正则表达式之后,我得到了字符串- some text (some text) some但是这不适用于这样的字符串- some text (some text) some (text (text) some). How to fix it? 如何解决?

"How to fix it?" “如何解决?” Traditional answer: You can't. 传统答案:您不能。 Regular expressions do not support rested constructs. 正则表达式不支持休息结构。 This is true for most regex dialects out there. 对于大多数正则表达式来说都是如此。


The .NET regular expression engine however supports balancing groups . 但是.NET正则表达式引擎支持平衡组 With them you can identify and handle nesting. 使用它们,您可以识别和处理嵌套。

To handle a nested construct, you must define its opening and closing pattern, in your case those are the parentheses ( and ) , respectively. 要处理嵌套结构,必须定义其打开和关闭模式,在这种情况下,分别是括号()

  • open: (?<paren>\\() 打开: (?<paren>\\()
  • close: (?<-paren>\\)) 关闭: (?<-paren>\\))

Think of this as a sort of counter named "paren" that counts up when it encounters ( and counts down when it encounters ) (internally, it's a bit different, but as a metaphor this is sufficient). 可以将其视为一种名为“ paren”的计数器,该计数器在遇到时递增计数(遇到时递减计数) (内部有点不同,但是作为一个隐喻,这就足够了)。

Now those two can be used to define the contents of a parenthesis, ie 现在,可以使用这两个定义括号的内容,即

  • either anything but a parenthesis: [^()]* 除括号外的任何内容: [^()]*
  • or the opening pattern 或开放模式
  • or closing pattern from above 或从上方关闭模式

or, in one expression: (?:[^()]*|(?<paren>\\()|(?<-paren>\\)))+ 或在一个表达式中: (?:[^()]*|(?<paren>\\()|(?<-paren>\\)))+

The entire regex should fail when the counter is not zero at the end, ie the parentheses are not balanced. 当计数器的末尾不为零(即括号不平衡)时,整个正则表达式将失败。 To make that happen, the (?(paren)(?!)) construct is used (that's a conditional , designed to fail when there is an unmatched paren left). 为此,使用(?(paren)(?!))构造(这是一个有条件的 ,旨在在paren不匹配时失败。

Your finished expression looks like this (whitespace ignored) 完成的表达式如下所示(忽略空格)

\(
  (?:
    [^()]*
    |(?<paren>\()
    |(?<-paren>\))
  )+
  (?(paren)(?!))
\)\.$

See it live: http://regexhero.net/tester/?id=feb992a2-cc5d-497a-9d4a-a10317487e46 现场观看: http//regexhero.net/tester/? id = feb992a2-cc5d-497a-9d4a-a10317487e46

Recommended reading: 推荐阅读:

Just change your regex like below to match the brackets which ends with . 只需如下更改您的正则表达式,以匹配以结尾的括号.

@"\((?:[^()]*\([^()]*\))*[^()]*\)\."

DEMO 演示

Regular Expression: 正则表达式:

\(                       '('
(?:                      group, but do not capture (0 or more
                         times):
  [^()]*                   any character except: '(', ')' (0 or
                           more times)
  \(                       '('
  [^()]*                   any character except: '(', ')' (0 or
                           more times)
  \)                       ')'
)*                       end of grouping
[^()]*                   any character except: '(', ')' (0 or more
                         times)
\)                       ')'
\.                       '.'

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

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