正则表达式 - 捕获具有重叠的括号之间的多个条目

时间:2013-04-12 19:28:45

标签: regex sed awk grep

我有一个像

这样的字符串
.....((((...)))...((((..)))).... 

我希望拥有 分别为.....((((...)))......((((..)))).... ..

我想出了/[.(]*(.?)[.)]*/输出

.....((((...)))...((((...)))...

请注意,我希望第一次切割的右端有三个点位于第二个切口的左侧部分。

感谢任何输入!

3 个答案:

答案 0 :(得分:3)

$ cat file
.....((((...)))...((((..))))....

$ sed -r 's/([^)]+[)]+([^(]+))/\1 \2/' file
.....((((...)))... ...((((..))))....

或者如果你的sed不支持ERE(-r选项),那么你可以这样做:

$ sed 's/\([^)]*[)]*\([^(]*\)\)/\1 \2/' file
.....((((...)))... ...((((..))))....

语义稍有不同(* = 0或更多,而+ = 1或更多),但你的例子似乎没问题。

编辑:按要求说明:

sed -r ' # use sed with Extended Regular Expressions enabled so "+" works.
s/       # search command and search start delimiter
(        # start of 1st saved RE-matching string (\1 later) which will contain:
[^)]+    #    1 or more non-) characters (e.g. "."s) then
[)]+     #    1 or more )s then
(        #    start of 2nd saved RE-matching string (\2 later) which will contain:
[^(]+    #        1 or more non-) characters (e.g. "."s)
)        #    end of 2nd saved RE-matching string so by this point \2 = "..."
)        # end of 1st saved RE-matching string so by this point \1 = ".....((((...)))..."
/        # search end delimiter and replacement start delimiter
\1 \2    # print \1=".....((((...)))..." then a space then \2="..." then the remaining unmatched input text="((((..))))...."
/        # replacement end delimiter
' file   # operate on the contents of file "file"

答案 1 :(得分:1)

我认为你必须分3步完成。原因是你在两个输出中都是中间的“......”,我怀疑它在单个正则表达式命令中是可能的。注意:我正在使用“。(。)。”引用你特定的字符串输出模式。

第1步:匹配“。(。)。”并返回第一个输出 第2步:匹配第一个“。(。)”并将其从字符串中删除 步骤3:将步骤2中的剩余字符串与“。(。)”匹配。并返回其输出。

我在rubular.com上玩它,我得到的正则表达式与你的不同。
/(.*(+.*)+.*)(.*(+.*)+.*)/这不是你想要的 /(.*(+.*)+.*)/这将匹配个人“。(。)。”

答案 2 :(得分:0)

HM1是对的,你不能在RE中匹配两次chars。 一个想法是分开匹配共同部分和concat之后。 gawk的一个例子:

echo "begin(first round bracket)middle(second round bracket)end" | gawk 'match($0, /^([^)]+\))([^(]+)(.+)$/, a) { first=a[1] a[2]; second=a[2] a[3]; print first "\n" second }'