Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.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
如何从regex获取所有匹配项?_Regex_Bash_Shell - Fatal编程技术网

如何从regex获取所有匹配项?

如何从regex获取所有匹配项?,regex,bash,shell,Regex,Bash,Shell,我想获取所有出现的[0-9A-Z]+?以供以后处理。 我确实有 if [[ `cat file` =~ '[0-9A-Z]+?' ]]; then echo $BASH_REMATCH; fi 这给了我第一个匹配,但我如何处理文件中的所有匹配 谢谢如果您只想获得正则表达式的匹配文本,我将使用 grep -o 'regex' file 本着你代码的精神,我会将其修改为 while read line; do [[ $line =~ regex ]] || continue

我想获取所有出现的
[0-9A-Z]+?
以供以后处理。 我确实有

if [[ `cat file` =~ '[0-9A-Z]+?' ]]; then
  echo $BASH_REMATCH;
fi
这给了我第一个匹配,但我如何处理文件中的所有匹配


谢谢

如果您只想获得正则表达式的匹配文本,我将使用

grep -o 'regex' file
本着你代码的精神,我会将其修改为

while read line; do
    [[ $line =~ regex ]] || continue
    # do somethinw with $line or $BASH_REMATCH, perhaps put it in an array.
done < file
读行时
;做
[$line=~regex]| |继续
#使用$line或$BASH_重新匹配,或者将其放入数组中。
完成<文件
如果您想在同一行上匹配多个正则表达式,这里有一种方法

while read line; do
    # do somethinw with $line
done < <(grep -o 'regex' file)
读行时
;做
#用$line做点什么

done<你的意思是添加新的吗?在模式的末尾,正则表达式稍微复杂一些,+?需要懒惰。尽管“while…done