regex Bash正则表达式匹配[已关闭]

dohp0rv5  于 7个月前  发布在  其他
关注(0)|答案(1)|浏览(84)

**已关闭。**此问题需要debugging details。目前不接受回答。

编辑问题以包括desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem。这将帮助其他人回答问题。
25天前关闭
Improve this question
我想准备一个脚本来分析bash中的一些日志文件。文件中的行看起来像:... Error(...), Error(...)...我想做一些像:if [[ $line =~ $regex ]]的事情来处理多个匹配,但我找不到这样做的方法。有什么想法吗?
PS:${BASH_REMATCH[1]}不是一个解决方案,因为它包含组匹配信息,而不是匹配本身。

3pvhb19x

3pvhb19x1#

如果你把所有提到的代码片段放在一起,你只需要Error正则表达式和一个while-loop来获取一行中的每个Error:

#!/bin/bash

line="... Error(foo), Error(bar), Error(foobar) ..."

regex="Error\(([^)]+)\)"

while [[ $line =~ $regex ]]; do
    echo "Found match: ${BASH_REMATCH[1]}"
    # Remove the found match from the line to continue searching
    line=${line#*${BASH_REMATCH[1]}}
done

字符串
在线测试:https://onlinegdb.com/MUPXbg7ZyM

相关问题