简体   繁体   English

shell命令:需要有关正则表达式匹配的帮助

[英]shell command: need help about a regex match

I have a sentence that I want to write a shell command to grep it from a text: The sentence is: 我有一个句子,我想编写一个shell命令以从文本中对它进行grep:该句子是:

self.timeout=2.0

However, as this is a part of code from a file. 但是,由于这是文件中代码的一部分。 so it this sentence could also be 所以这句话也可能是

self.timeout = 2.0

or 要么

self.timeout =2.0

or 要么

self.timeout = 8.0

that is: there may be blanks besides "=", and the value of self.timeout maybe different. 也就是说:“ =”以外可能还有空格,并且self.timeout的值可能不同。

So could anybody help to give me a regex in shell command. 所以任何人都可以帮助在shell命令中给我一个正则表达式。 Anyway, I know the shell: 无论如何,我知道外壳:

grep "self.timeout*="

works. 作品。 But I think it is not a good regex in shell command. 但是我认为在shell命令中这不是一个很好的正则表达式。

Thanks a lot! 非常感谢!

I'd do: 我会做:

grep -E 'self\.timeout[ \t]*=[ \t]*[0-9.]+'

note: 注意:

      |      |          |  |           
  use egrep  |          | zero or more
             |    whitespace
             |
  make sure we're matching
     a dot instead of
      "any character"     

Using grep -E aka egrep you can use a regular expression, with which the * operator will match 0 or more of the preceding character: 使用grep -E aka egrep您可以使用正则表达式,使用*运算符可以匹配0个或多个前面的字符:

egrep 'self\.timeout *='

Or use [[:space:]] to match all whitespace characters: 或使用[[:space:]]匹配所有空白字符:

egrep 'self\.timeout[[:space:]]*='

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

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