Regex 缺少正则表达式中的最后一个右括号

Regex 缺少正则表达式中的最后一个右括号,regex,string,Regex,String,从下面的字符串 lookup('CONTACT','CON_LKP','LKP_TAB1.COUNTRY_CD')||lookup('CONTACT','CON_LKP','LKP_TAB2.OBJECTIVE')||$country_cd$ 要提取我使用的查找函数 Pattern p = Pattern.compile("(lookup\\([^)]*)\\)"); 但此函数正在重新匹配,不包括最后一个结束括号'。喜欢它的回归 lookup('CONTACT','CON_LKP','LKP

从下面的字符串

lookup('CONTACT','CON_LKP','LKP_TAB1.COUNTRY_CD')||lookup('CONTACT','CON_LKP','LKP_TAB2.OBJECTIVE')||$country_cd$
要提取我使用的查找函数

Pattern p = Pattern.compile("(lookup\\([^)]*)\\)");
但此函数正在重新匹配,不包括最后一个结束括号'。喜欢它的回归

lookup('CONTACT','CON_LKP','LKP_TAB1.COUNTRY_CD'

我犯错误的地方。顺便说一下,我对正则表达式知之甚少。所以我的问题可能很傻。

把这行改成

Pattern p = Pattern.compile("(lookup\\([^)]*\\))");
您还需要在匹配组中包括端括号
\\)

代码:

shadyabhi@archlinux /tmp $ cat RegExpTest.java
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegExpTest {
    public static void main(String[] args) {

        String str = "lookup('CONTACT','CON_LKP','LKP_TAB1.COUNTRY_CD')||lookup('CONTACT','CON_LKP','LKP_TAB2.OBJECTIVE')||$country_cd$";

        String p = "(lookup\\([^)]*\\))";
        Pattern pattern = Pattern.compile(p);
        Matcher matcher = pattern.matcher(str);
        if (matcher.find()){
            System.out.println(matcher.group(1));
        }
    }
}
shadyabhi@archlinux /tmp $ javac RegExpTest.java 
shadyabhi@archlinux /tmp $ java RegExpTest 
lookup('CONTACT','CON_LKP','LKP_TAB1.COUNTRY_CD')
shadyabhi@archlinux /tmp $