使用perl在一行中查找两组括号的正则表达式是什么? [英] what's the regular expression to find two sets of parenthesis in a row using perl?

查看:54
本文介绍了使用perl在一行中查找两组括号的正则表达式是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有带有不同括号集的行.例如

I have rows with different sets of parenthesis. For example

     (sdfsfs) (sfdsfd) 
     (sdfsfs) (sfdsfd) (sfdsfd) 
     (sdfsfs) (sfdsfd) (sfdsfd) (sfdsfd)
     (sdfsfs) (sfdsfd) (sfdsfd) (sfdsfd) (sfdsfd)
     (sdfsfs) (sfdsfd) (sfdsfd) (sfdsfd) (sfdsfd) 

只查找带有两组括号的行的正则表达式是什么?在我上面的例子中,第一行.

What's the regular expression to find only rows with two sets of parenthesis? In my example above, the first row.

当试试这个正则表达式

/\(.*\) \(.*\)/

它返回所有行

推荐答案

您可以使用此正则表达式来匹配恰好包含 2 个括号集的行:

You can use this regex to match a line with exactly 2 parentheses sets:

m/^(\([^)]*\)\s*){2}$/

或:

m/^\([^)]*\)\s*\([^)]*\)$/

正则表达式演示

[^)]* 将匹配除 ) 之外的任何字符,并且比 .*?.*<更有效/代码>.

[^)]* will match any character but ) and is more efficient than .*? or .*.

这篇关于使用perl在一行中查找两组括号的正则表达式是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆