String exe = "(com.nhncorp.lucy.db.sql.ManagedDataSource:677) executing ibatis statement 'com.nhn.sapphire.cache.selectOnlyInfo'"; String reg = "(?<=')[^']*";Matcher mer = Pattern.compile(reg).matcher(exe); if (mer.find()) { System.out.println(mer.group()); }
我也是最近刚学的,?表达式意思是作为判断依据,但并不作为匹配范围,具体可参见JDK文档。Special constructs (non-capturing) (?:X) X, as a non-capturing group (?idmsux-idmsux) Nothing, but turns match flags i d m s u x on - off (?idmsux-idmsux:X) X, as a non-capturing group with the given flags i d m s u x on - off (?=X) X, via zero-width positive lookahead (?!X) X, via zero-width negative lookahead (?<=X) X, via zero-width positive lookbehind (?<!X) X, via zero-width negative lookbehind (?>X) X, as an independent, non-capturing group
String exe = "(com.nhncorp.lucy.db.sql.ManagedDataSource:677) executing ibatis statement 'com.nhn.sapphire.cache.selectOnlyInfo'"; String reg = "\'(.*)\'";Matcher mer = Pattern.compile(reg).matcher(exe); if (mer.find()) { System.out.println(mer.group(1));//直接用group()会带出单引号 }
String reg = "(?<=')[^']*";Matcher mer = Pattern.compile(reg).matcher(exe);
if (mer.find()) {
System.out.println(mer.group());
}
(?:X) X, as a non-capturing group
(?idmsux-idmsux) Nothing, but turns match flags i d m s u x on - off
(?idmsux-idmsux:X) X, as a non-capturing group with the given flags i d m s u x on - off
(?=X) X, via zero-width positive lookahead
(?!X) X, via zero-width negative lookahead
(?<=X) X, via zero-width positive lookbehind
(?<!X) X, via zero-width negative lookbehind
(?>X) X, as an independent, non-capturing group
String reg = "\'(.*)\'";Matcher mer = Pattern.compile(reg).matcher(exe);
if (mer.find()) {
System.out.println(mer.group(1));//直接用group()会带出单引号
}
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(str);
while (matcher.find()) {
System.out.println(matcher.group(1))
}
(?<=) 这个学名叫作 positive lookbehind(肯定型逆序环视)参考这个帖子我在 4 楼的回复:http://topic.csdn.net/u/20100331/19/c0f55c3e-9338-4622-9a59-025672dfd6e4.html
String regex = "'(.*?)'";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(str);
while (matcher.find()) {
System.out.println(matcher.group(1));
}
return matcher.group(1)就不会有两边的单引号了,原来是return matcher.group()