Understanding sed regex with numbered groups
\(ADDR=[^|]*\) |.*/\1/
Here
-
[^|]
matches anything other than|
, and the quantifier*
specifies zero or more of its occurrences.^
in a character class negates the character class. -
|
matches the character|
NOTE In sed
metacharacters like |
(
)
etc. lose their meaning, therefore |
it is not an interleaving, but matches a symbol |
. If you want to treat metacharacters as such, then -r
(extended regex) will do it (with GNU sed
, use -E
with BSD sed
). Or run \|
.
Example:
$ echo "hello ADDR= hello | world " | sed 's/.*\(ADDR=[^|]*\) |.*/\1/'
ADDR= hello
Here (ADDR=[^|]*\)
matches ADDR= hello
that contains anything other than |
.
source to share
[^...]
Matches any single character that does not belong to a class.
|
A vertical bar separates two or more alternatives. Matching occurs if any of the alternatives are met. For example, gray|grey
matches both gray
and grey
.
[^|]
matches anything other than a |
. ^
in a character class negates the character class and |
is lost in real meaning when used with sed
.
source to share