如何对包含多行的正则表达式组执行多个查找? 平台:Java 当前模式:(/[^::()]+\.[^::()]+)。(\d.*):(\d.*)。((?:警告|错误|注意):。+) 问题:对于第4组,它只匹配到行尾。如果我添加DOTALL,它只会找到一组具有上次查找描述的组
我想找到匹配项的输入-进入如何对包含多行的正则表达式组执行多个查找? 平台:Java 当前模式:(/[^::()]+\.[^::()]+)。(\d.*):(\d.*)。((?:警告|错误|注意):。+) 问题:对于第4组,它只匹配到行尾。如果我添加DOTALL,它只会找到一组具有上次查找描述的组,java,regex,Java,Regex,我想找到匹配项的输入-进入模式。编译(字符串): Build failed: Command failed with exit code 1. stderr: /sample/path/to/SampleFile.java:1: error: [PackageLocation] Expected package /sample/path/to/ to be declared in a directory ending with /sample/path/to, instead found /sa
模式。编译(字符串)
:
Build failed: Command failed with exit code 1.
stderr: /sample/path/to/SampleFile.java:1: error: [PackageLocation] Expected package /sample/path/to/ to be declared in a directory ending with /sample/path/to, instead found /sample/path/To
package sample.path.to;
^
(see http://errorprone.info/bugpattern/PackageLocation)
/sample/path/to/SampleFile2.java:-1: note: Some input files use or override a deprecated API.
/sample/path/to/SampleFile2.java:-1:6 note: Recompile with -Xlint:deprecation for details.
预期输出:
Build failed: Command failed with exit code 1.
stderr: /sample/path/to/SampleFile.java:1: error: [PackageLocation] Expected package /sample/path/to/ to be declared in a directory ending with /sample/path/to, instead found /sample/path/To
package sample.path.to;
^
(see http://errorprone.info/bugpattern/PackageLocation)
/sample/path/to/SampleFile2.java:-1: note: Some input files use or override a deprecated API.
/sample/path/to/SampleFile2.java:-1:6 note: Recompile with -Xlint:deprecation for details.
找到0
- 组1:/sample/path/to/SampleFile.java
- 第2组:1
- 第3组:null
- 第4组:错误:[PackageLocation]应在以/sample/path/to结尾的目录中声明package/sample/path/to/而不是找到/sample/path/to; ^ (见附件)
- 组1:/sample/path/to/SampleFile2.java
- 第2组:1
- 第3组:null
- 第4组:注意:某些输入文件使用或覆盖不推荐使用的API
- 组1:/sample/path/to/SampleFile2.java
- 第2组:1
- 第3组:6
- 第4组:注意:使用-Xlint重新编译:有关详细信息,请参阅弃用
解释 我决定使用双换行符(因为这似乎是3行之间的常用分隔符),而不是依靠行尾来确定匹配的结束。我将重点解释我添加/修改的部分
*
更改为:-?
- 虽然这可能是一个小的变化,但它可能会帮助您在将来不捕获字符串的错误部分
(?:(?![\r\n]{2})[\s\s])+
匹配以下一次或多次()
负向前看确保后面不是两个换行符(?![\r\n]{2})
匹配任何字符(包括换行符)[\s\s]
插入符号实际上在输入中,还是为了显示没有被捕获的内容?插入符号(\n)实际上在输入中,所以插入符号实际上不在输入中,它只是一个换行符?伙计们,错误是我想用
pattern.matcher(string)查找匹配的字符串
您显示的运行时错误告诉您有一个命名错误的包。它与你的正则表达式或其他输入无关。你一定明白了!多谢各位much@HumbleStudent我在上面加了一个解释,非常感谢!