获取文本已经由正则表达式

心中已经发现了用于本地化字符串,项目像这样:获取文本已经由正则表达式

{{ "I'd like this quote to be localized" | trans }} 

{{ "this_is_a_key" |trans}}

{{'another text'| trans}}

{{"ohter"|trans }}

{{'no_spaces'|trans}}

使用这个命令:

find src/ -type f -name '*.twig' -execdir egrep -o -- "\{\{[^|()}{]*\|[(trans) ^}]*\}\}" {} \;

我可以在我的各种模板文件中找到所有这些,女巫已经很棒了。

但是我想更进一步,直接获取上面例子中的“”或“'之间的内容。如何自定义我的搜索或进行其他搜索后提取?

PS:我现在.SH脚本是:

old_IFS=$IFS 

IFS=$'\n'

transFound = 0;

allUsed=`find src/ -type f -name '*.twig' -execdir egrep -o -- "\{\{[^|(){]*\|[(trans) ^}]*\}\}" {} \;`

defaultFile="messages.yml"

for key in $allUsed

do

echo $key

let "transfound++"

done

echo "Found $transfound translations"

回答:

我终于找到它;)只好用SED:

find src/ -type f -name '*.twig' -execdir egrep -o -- "\{\{ ?['\"][^|()}{]*\|[(trans) ^}]*\}\}" {} \; | sed "s/.*'\(.*\)'[^']*$/\1/" | sed 's/.*"\(.*\)"[^"]*$/\1/' 

回答:

您可以使用perl(-P)正则表达式,和only(-o)打印出匹配

$ grep -oP "(['\"]).*\1" input.txt 

"I'd like this quote to be localized"

"this_is_a_key"

'another text'

"ohter"

'no_spaces'


-o, --only-matching 

Print only the matched (non-empty) parts of a matching line, with each such part on a separate output line.

-P, --perl-regexp

Interpret PATTERN as a Perl regular expression (PCRE, see below). This is highly experimental and grep -P may warn of unimplemented

features.

以上是 获取文本已经由正则表达式 的全部内容, 来源链接: utcz.com/qa/258071.html

回到顶部