2

What does this regex replace :

\([^:]*\):\(.*\)
  • Usually used to extract usernames in e.g /etc/passwd file. See regex101.com and put there ([^:]*):(.*). – Valentin Bajrami Oct 19 '17 at 9:07
1

That's probably removing only first colon within a string.

sed 's/\([^:]*\):\(.*\)/\1\2/' <<<"Hello:Unix:Users"
HelloUnix:Users

This \([^:]*\) matches anything until which is not a colon (or until a colon not seen).

Then match ...\):\(... that colon. and everything \(.*\) after that. But when \(..\) 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 : )

  • 1
    If someone wants to remove the first colon, sed 's/://' would be the better choice. But maybe the order is to be changed at the same time: sed 's/\([^:]*\):\(.*\)/\2--\1/' or something like that. Btw, I think you speak of backreferences only if \1 and so on are used in the regexp itself, not in the replacement string. – Philippos Oct 19 '17 at 9:07
3

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 passwd password.

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.

First part:

    sed 's/\([^:]*\):    
          /\([^:]*\): Match the string till you see a colon `:` and group.

Second part:

   sed \(.*\)/\1/' /etc/passwd
       \(.*\)  Match everything after `:` and group

So now we've 2 groups where we can back-reference using \1 which indicates we want to reference to the first group and print those results.

2

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.

With 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
1

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 \1 and \2, or in some other variable or array depending on what tool is being used.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.