Regex for The Same Pattern Multiple Times in One Line

好久不见. 提交于 2021-01-27 16:25:32

问题


The pattern I'm looking for is this:

TXT.*\.txt

That pattern can occur multiple times in any given line. I would like to either extract each instance of the pattern out or alternatively delete the text that surrounds each instance using sed (or anything, really).

Thanks!


回答1:


You can use grep as:

grep -o 'TXT[^.]*\.txt' file



回答2:


You can use Perl as:

$ cat file
foo TXT1.txt bar TXT2.txt baz
foo TXT3.txt bar TXT4.txt baz

$ perl -ne 'print "$1\n" while(/(TXT.*?\.txt)/g)' file
TXT1.txt
TXT2.txt
TXT3.txt
TXT4.txt
$ 


来源:https://stackoverflow.com/questions/4341309/regex-for-the-same-pattern-multiple-times-in-one-line

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!