简体   繁体   English

删除最外面的括号

[英]Remove outermost parentheses

I am having problem while removing outermost parentheses from a string using GREL. 我在使用GREL从字符串中删除最外面的括号时遇到问题。 What i am trying to do is simply remove outermost parentheses and all other parentheses should be kept intact. 我想要做的只是删除最外面的括号,所有其他括号应保持不变。 Below is what i am trying to do using regex - 以下是我正在尝试使用正则表达式 -

value.split(/(abc+/)

and below is my sample string that i am trying to parse and desired output. 以下是我试图解析和期望输出的示例字符串。

Foo ( test1 test2 ) => Foo test1 test2
Bar ( test1 t3() test2 ) => Bar test1 t3() test2
Baz ((("Fdsfds"))) => Baz (("Fdsfds"))

I would appreciate any help. 我将不胜感激任何帮助。

One option could be to use a capturing group and in the replacement use the first capturing group. 一种选择可以是使用捕获组,而在替换中使用第一个捕获组。

Note that this does not take balanced parenthesis into account. 请注意,这不考虑平衡括号。

It matches the outer parenthesis, then captures in a group what is inside and matches an outer parenthesis again. 它匹配外括号,然后在组中捕获内部的内容并再次匹配外括号。

Inside the capturing group is an alternation that matches not () or from an openening till closing parenthesis. 捕获组内部是一个与not ()匹配或从opennening到右括号匹配的替换。

\((\(*(?:[^)(]*|\([^)]*\))*\)*)\)

Explanation 说明

  • \\( Match outer parenthesis ( \\(匹配外括号(
  • ( Capture group - \\(* Match 0+ times ( (捕获组 - \\(*匹配0次以上(
    • (?: Non capturing group (?:非捕获组
      • [^)(]* Match 0+ times not ( or ) [^)(]*匹配0次以上不()
      • | Or 要么
      • \\([^)]*\\) Match from ( till next ) closing parenthesis \\([^)]*\\)匹配(直到下一个)右括号
    • )* Close non capturing group and repeat 0+ times )*关闭非捕获组并重复0次以上
    • \\)* Match 0+ times a closing parenthesis \\)*匹配括号的0+倍
  • ) Close capture group )关闭捕获组
  • \\) Match outer closing parenthesis \\)匹配外部右括号

Regex demo 正则表达式演示

I am not familiar at all with grel or openrefine regex syntax or limitation, in a broader context you can use one of the following regexes depending on what regex engine is used. 我对grelopenrefine正则表达式语法或限制完全不熟悉,在更广泛的上下文中,您可以使用以下正则表达式之一,具体取决于使用的正则表达式引擎。

PCRE regex: PCRE正则表达式:

^[^(]*\K\(|\)(?!.*\).*)

demo: https://regex101.com/r/lj0Qbl/1/ 演示: https//regex101.com/r/lj0Qbl/1/

ECMA regex: ECMA正则表达式:

(?<!.*\(.*)\(|\)(?!.*\).*)

demo: https://regex101.com/r/g8XbjI/1/ 演示: https//regex101.com/r/g8XbjI/1/

for both: 对彼此而言:

Input: 输入:

Foo ( test1 test2 ) bob
Bar ( test1 t3() test2 ) bob
Baz ((("Fdsfds"))) bob

Output: 输出:

Foo  test1 test2  bob
Bar  test1 t3() test2  bob
Baz (("Fdsfds")) bob

If our inputs are all similar to those listed in the question, this expression might work: 如果我们的输入都与问题中列出的输入类似,则此表达式可能有效:

(.+?)\s+\((\s+)?(.*)(\s+)?\)

and we would replace it with $1 $3 . 我们将以$1 $3替换它。

Demo 演示

RegEx Circuit RegEx电路

jex.im visualizes regular expressions: jex.im可视化正则表达式:

在此输入图像描述

There are multiple ways of doing this with GREL. 使用GREL有多种方法可以做到这一点。 If you want to use regular expressions then in GREL I'd recommend combining this with the GREL match function: 如果你想使用正则表达式,那么在GREL中我建议将它与GREL match函数结合使用:

value.match(/(.*?)\((.*)\)(.*)/).join("")

Note that this expression assumes there is at least one pair of parentheses in the cell - cells that have no parentheses or have only an opening or only a closing parentheses will give an error - however you can use the option 'on error keep original' (which is the default) when doing a cell transformation to keep the original value in these cases 请注意,此表达式假定单元格中至少有一对括号 - 没有括号或只有一个开口或只有一个右括号的单元格会出错 - 但是你可以使用'on error keep original'选项(这是默认情况下进行单元格转换以保持原始值在这些情况下

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

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