この正規表現は何を置き換えますか?
\([^:]*\):\(.*\)
ベストアンサー1
この正規表現は通常、/etc/passwd
ファイルからユーザー名を抽出するために使用されます。例えば
sed 's/\([^:]*\):\(.*\)/\1/' /etc/passwd
パスワードのみに基づいてユーザー名を返しますpasswd
。
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
要するに。
最初の部分:
sed 's/\([^:]*\):
/\([^:]*\): Match the string till you see a colon `:` and group.
2番目の部分:
sed \(.*\)/\1/' /etc/passwd
\(.*\) Match everything after `:` and group
2つのグループがあるので、それをback-reference
使用して\1
最初のグループを参照し、その結果を印刷しようとしていることを示します。