简体   繁体   English

终端中的grep和regex-在首次出现字符时停止

[英]grep & regex in terminal - stop at first occurence of character

I am trying to match phrases like translatedString:@"the string"] 我正在尝试匹配类似translationString:@“ the string”]的短语

I have entered the following in Terminal, which works but brings back everything in the line after the match 我已经在Terminal中输入了以下内容,该方法可以正常工作,但是在比赛之后将行中的所有内容恢复

grep -oh 'translatedString:.*]' ArticleV.m

How can I adjust the expression so that it only returns matches until the ] is found? 如何调整表达式,使其仅在找到]之前返回匹配项?

.* matches greedily. .*贪婪地匹配。 Use .*? 使用.*? (and an extended regular expression) instead: (以及扩展的正则表达式)代替:

grep -ohE 'translatedString:.*?]' ArticleV.m

If you don't like the -E flag, egrep works the same way: 如果您不喜欢-E标志,则egrep工作方式相同:

egrep -oh 'translatedString:.*?]' ArticleV.m

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM