简体   繁体   中英

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:

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.

So could anybody help to give me a regex in shell command. Anyway, I know the shell:

grep "self.timeout*="

works. But I think it is not a good regex in shell command.

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:

egrep 'self\.timeout *='

Or use [[:space:]] to match all whitespace characters:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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