简体   繁体   中英

Two regular expressions in grep with alternation

'^\{[a-z]*:[0-9]*\}$|;^[a-z]=[0-9]$' 

What's wrong? The documentation (man page) said that | is alternation operator.

From the man page:

Alternation
Two regular expressions may be joined by the infix operator |; the resulting regular expression matches any string matching either alternate expression.

You are misinterpreting this, it is not saying that |; is the alternation operator, it is saying that | is the alternation operator, the semi-colon separates the two parts of the sentence.

Also, unless you are using the extended regex option ( -E ) you will need to escape the | :

Basic vs Extended Regular Expressions
In basic regular expressions the meta-characters ?, +, {, |, (, and ) lose their special meaning; instead use the backslashed versions \\?, \\+, \\{, \\|, \\(, and \\).

The end result might look something like this:

grep -E '^\{[a-z]*:[0-9]*\}$|^[a-z]=[0-9]$' some_file

Or without the -E option:

grep '^{[a-z]*:[0-9]*}$\|^[a-z]=[0-9]$' some_file

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