Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/396.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将简单Grep转换为Java_Java_Regex - Fatal编程技术网

将简单Grep转换为Java

将简单Grep转换为Java,java,regex,Java,Regex,寻找 grep -aiH '^WARN\|^ERROR\|^FATAL' myLogFile.log | \ ... 我写 if (!line.startsWith("ERROR") || !line.startsWith("WARN") || !line.startsWith("FATAL")) { ... 但是,如何考虑“i”标志,使比较不区分大小写?使用此方法,首先将行转换为大写字符串: () 因此,不要使用!line.startsWith(…)write!li

寻找

grep -aiH '^WARN\|^ERROR\|^FATAL' myLogFile.log | \
...
我写

if (!line.startsWith("ERROR") || !line.startsWith("WARN") 
           || !line.startsWith("FATAL")) {
...

但是,如何考虑“
i
”标志,使比较不区分大小写?

使用此方法,首先将行转换为大写字符串:

()


因此,不要使用
!line.startsWith(…)
write
!line.toUpperCase().startsWith(…)

只需先将行转换为大写字符串,使用以下方法:

()


因此,不要使用
!line.startsWith(…)
write
!line.toUpperCase().startsWith(…)

也可能希望查看commons lang StringUtils:

例如
!StringUtils.StartWithIgnoreCase(第行,“错误”)

这也有空安全的好处;如果
line
为null,它将返回false,而不是抛出NullPointerException


最后,您还可以尝试类似于
!line.matches((?i)(警告|错误|致命)。*”

也可能希望查看commons lang StringUtils:

例如
!StringUtils.StartWithIgnoreCase(第行,“错误”)

这也有空安全的好处;如果
line
为null,它将返回false,而不是抛出NullPointerException

最后,您还可以尝试类似于
!行匹配((?i)(警告|错误|致命)。*”