我正在尝试匹配某些东西,对于我的生活,我无法弄清楚为什么它不能按预期工作。
细绳:"/*! preserved */"
图案:
Pattern.compile("(/[*][!](?:.+?)[*]/)", Pattern.MULTILINE | Pattern.DOTALL | Pattern.CASE_INSENSITIVE)
- FAIL
图案:
Pattern.compile("(/[*](?:.+?)[*]/)", Pattern.MULTILINE | Pattern.DOTALL | Pattern.CASE_INSENSITIVE)
- PASS
我只想捕捉!
在开始评论字符串之后有权利的评论。我什至尝试将比赛减少到/*!
:
Pattern.compile("(/[*][!])", Pattern.MULTILINE | Pattern.DOTALL | Pattern.CASE_INSENSITIVE)
- FAIL
Pattern.compile("(/[*!]{2})", Pattern.MULTILINE | Pattern.DOTALL | Pattern.CASE_INSENSITIVE)
- FAIL
Pattern.compile("(/[*][!])", Pattern.MULTILINE | Pattern.DOTALL | Pattern.CASE_INSENSITIVE)
- FAIL
Java Version "1.6.0_43"
Java(TM) SE Runtime Environment (build 1.6.0_43-b01)
Java HotSpot(TM) 64-Bit Server VM (build 20.14-b01, mixed mode)
编辑
这失败了,这对我来说没有意义,因为我的模式没有要求的开始或结束:
Pattern p = Pattern.compile("(/[*]!.+?[*]/)", Pattern.MULTILINE | Pattern.DOTALL | Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher("this is a test /*! preserved */ this is a test /*! preserved1 */");
System.out.println("Pattern: " + p);
System.out.println("Group: " + m.group(1));
System.out.println("Found: " + m.find());
即使将其减少为仅匹配也会/*!
失败:
Pattern p = Pattern.compile("(/[*]!)", Pattern.MULTILINE | Pattern.DOTALL | Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher("this is a test /*! preserved */ this is a test /*! preserved1 */");
System.out.println("Match: " + m.find());
System.out.println("Pattern: " + p);
System.out.println("Group: " + m.group(1));