开发者

Regex for The Same Pattern Multiple Times in One Line

开发者 https://www.devze.com 2023-01-28 07:15 出处:网络
The pattern I\'m looking for is this: TXT.开发者_JS百科*\\.txt That pattern can occur multiple times in any given line. I would like to either extract each instance of the pattern out or alternativ

The pattern I'm looking for is this:

TXT.开发者_JS百科*\.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!


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
$ 


You can use grep as:

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

精彩评论

暂无评论...
验证码 换一张
取 消