understanding sed regex with numbered groups

2019-09-04 06:35发布

I'm trying to understand this sed regex.

       sed 's/.*\(ADDR=[^|]*\) |.*/\1/'

If I'm not wrong, the above will search for the pattern ADDR=<something> anywhere in a line and replace it with the first group. I don't get the meaning of [^|] here. Thanks for any help.

标签: regex linux sed
2条回答
疯言疯语
2楼-- · 2019-09-04 06:53

\(ADDR=[^|]*\) |.*/\1/

Here

  • [^|] matches anything other than | and the quantifier * quantifies zero or more occurrences of it. ^ in character class negates the character class.

  • | matches the character |

NOTE In sed metacharacters like | ( ) etc will lose its meaning so | is not an alternation but matches a | character. If you want to treat the metacharacters as such, then -r (extended regular expression) will do so (with GNU sed; use -E with BSD sed). Or escape \|.

Example:

$ echo "hello ADDR= hello | world " | sed 's/.*\(ADDR=[^|]*\) |.*/\1/'
ADDR= hello

Here (ADDR=[^|]*\) matches from ADDR= hello which contains anything other than |.

查看更多
该账号已被封号
3楼-- · 2019-09-04 07:01

[^...] Matches any single character that is not in the class.

| The vertical bar separates two or more alternatives. A match occurs if any of the alternatives is satisfied. For example, gray|grey matches both gray and grey.

[^|] matches anything other than a |. ^ in character class negates the character class while | is loose it's actual meaning when using with sed.

查看更多
登录 后发表回答