[英]shell command: need help about a regex match
我有一個句子,我想編寫一個shell命令以從文本中對它進行grep:該句子是:
self.timeout=2.0
但是,由於這是文件中代碼的一部分。 所以這句話也可能是
self.timeout = 2.0
要么
self.timeout =2.0
要么
self.timeout = 8.0
也就是說:“ =”以外可能還有空格,並且self.timeout的值可能不同。
所以任何人都可以幫助在shell命令中給我一個正則表達式。 無論如何,我知道外殼:
grep "self.timeout*="
作品。 但是我認為在shell命令中這不是一個很好的正則表達式。
非常感謝!
我會做:
grep -E 'self\.timeout[ \t]*=[ \t]*[0-9.]+'
注意:
| | | |
use egrep | | zero or more
| whitespace
|
make sure we're matching
a dot instead of
"any character"
使用grep -E
aka egrep
您可以使用正則表達式,使用*
運算符可以匹配0個或多個前面的字符:
egrep 'self\.timeout *='
或使用[[:space:]]
匹配所有空白字符:
egrep 'self\.timeout[[:space:]]*='
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.