What does this regex replace :
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.Sign up to join this community
What does this regex replace :
This regex is usually used to extract usernames from the
/etc/passwd file. For example
sed 's/\([^:]*\):\(.*\)/\1/' /etc/passwd
Will return usernames only from the
1st Capturing Group ([^:]*) Match a single character not present in the list below [^:]* * Quantifier — Matches between zero and unlimited times, as many times as possible, giving back as needed (greedy) : matches the character : literally (case sensitive) : matches the character : literally (case sensitive) 2nd Capturing Group (.*) .* matches any character (except for line terminators) * Quantifier — Matches between zero and unlimited times, as many times
So in simple terms.
sed 's/\([^:]*\): /\([^:]*\): Match the string till you see a colon `:` and group.
sed \(.*\)/\1/' /etc/passwd \(.*\) Match everything after `:` and group
So now we've 2 groups where we can
\1 which indicates we want to reference to the first group and print those results.
This regex do not execute replace operation, this is only regex without of context.
Need to know full command. But i can assume that this pattern accord to:
(any symbol except : repeated zero or more times):(any symbol remeated zero or more times)
So you can get a usernames from
/etc/passwd with this pattern.
sed you can use
-r option to omit
() and your pattern will simpliest. Insert your pattern in 's//\1/':
sed -r 's/([^:]*):(.*)/\1/' /etc/passwd
Output will be:
root daemon bin sys sync games
That's probably removing only first colon within a string.
sed 's/\([^:]*\):\(.*\)/\1\2/' <<<"Hello:Unix:Users" HelloUnix:Users
\([^:]*\) matches anything until which is not a colon (or until a colon not seen).
...\):\(... that colon. and everything
\(.*\) after that.
\(..\) is used, this is telling
sed to capture these as a group of match and their corresponding index (or in main
back-references) for first one will be
\1 and for next
\2 and etc.
I mentioned probably, because who want to use these in
sed replacement part is important, maybe s/he wanted to replace first colon with semi-colon.
sed 's/\([^:]*\):\(.*\)/\1;\2/' <<<"Hello:Unix:Users"
Or maybe s/he wanted to add another string between instead of a colon:
sed 's/\([^:]*\):\(.*\)/\1 Linux and \2/' <<<"Hello:Unix:Users"
Or whatever you can do and replace : )
It is a basic regular expression (as opposed to an extended one) that captures whatever is before the first colon, as well as everything that comes after it.
As an extended regular expression, it matches a parenthesised string before the a colon, and then another one after the colon, while assuming that the parenthesised bits of the input do not themselves contain parentheses.
Assuming it's a basic regular expression, it would capture the two strings
first part and
second part:third part in the input
first part:second part:third part
These two strings would be available in
\2, or in some other variable or array depending on what tool is being used.