[英]Find text enclosed by character multiples times
The problem: 问题:
Find pieces of text in a file enclosed by @
在
@
包围的文件中查找文本
Input: 输入:
@abc@ abc @ABC@
cba @cba@ CBA
Output: 输出:
@abc@ @ABC@
@cba@
I've tried the following: 我尝试了以下方法:
cat test.txt | perl -ne 'BEGIN { $/ = undef; } print $1 if(/(@.*@)/s)."\n"'
But this results in: 但这导致:
@abc@ abc @ABC@
cba @cba@
Additional: I was not complete. 另外:我还不完整。 The goal of the above is the replace the characters between the @ with something else: a should become chr(0x430) b should become chr(0x431) c should become chr(0x446) A should become chr(0x410) B should become chr(0x411) C should become chr(0x426) so with the above input in mind it should result in:
абц abc АБЦ
cba цба CBA
上面的目标是用其他字符替换@之间的字符:a应该变成chr(0x430)b应该变成chr(0x431)c应该变成chr(0x446)A应该变成chr(0x410)B应该变成chr( 0x411)C应该成为CHR(0x426),所以在考虑上述输入应该导致:
абц abc АБЦ
cba цба CBA
Sorry for my imcompleteness. 对不起,我不完整。 Thanks Kluther
谢谢克卢瑟
The problem with (@.*@)
is that *
is greedy: it matches the largest amount possible. (@.*@)
的问题在于*
贪婪:它与最大数量匹配。 Thus it will match everything between the first @
in the string and the last one. 因此,它将匹配字符串中第一个
@
和最后一个@
之间的所有内容。
You can make it non-greedy with (@.*?@)
. 您可以使用
(@.*?@)
使其不贪婪。 However, a better approach is to match everything that is not @
in between: 但是,更好的方法是匹配介于两者之间的所有
@
:
(@[^@]*@)
If you want to match every occurrence instead of the first one, you also need to use the /g
modifier and modify your code to use a loop: 如果要匹配每个匹配项而不是第一个匹配项,则还需要使用
/g
修饰符并修改代码以使用循环:
perl -ne 'BEGIN { $/ = undef; } print $1 while(/(\@[^@]*\@)/gs); print "\n"'
使用这样的模式
@[a-zA-Z]+@
使用此正则表达式:
cat test.txt | perl -pe 's/(?:(@ )|^[^@]).*?(?: (@)|$)/$1$2/g'
Use non-greedy search .+?
使用非贪婪搜索
.+?
or /(\\@([^@]*)\\@)/gsm
. 或
/(\\@([^@]*)\\@)/gsm
。
cat test.txt | perl -ne 'BEGIN { $/ = undef; } print $1." " while(/(\@([^@]*)\@)/gsm); print "\n";'
One way: 单程:
$ perl -pe '@a=$_=~/@[^@]+@/g; $_="@a";' file
@abc@ @ABC@ @cba@
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.