Regex not matching in sed
match, pattern-matching, regex, sed
Solution
Escape the braces otherwise sed's default behavior is to match `{} or ()` literally:
echo 'Hello, world!' | gsed '/^\w\{3\}lo, wor.*$/d'
Or else you need `-r` flag for extended regex capabilities:
echo 'Hello, world!' | gsed -r '/^\w{3}lo, wor.*$/d'
Problem
I have used regexs for years, and never come across this problem. On example websites (like http://regexone.com/lesson/1) where I can play around with what I'm trying to do, it matches, but in the unix shell using `sed`, it doesn't match. I discovered this when trying to write logcheck skipping rules. ``` $ echo 'Hello, world!' | sed '/^\w\w\wlo, wor.*$/d' $ ``` Works, but ``` $ echo 'Hello, world!' | sed '/^\w{3}lo, wor.*$/d' Hello, world! ``` doesn't. It doesn't see 3 alphanums with the {3} it seems. I found this out by trying to do reductions on ``` $ echo "Jul 15 11:31:08 gateway-laptop dbus[3076]: [system] Successfully activated service 'org.freedesktop.PackageKit'"|sed "/^\w{3} [ :0-9]{11} [._[:alnum:]-]+ dbus\[[0-9]+\]: \[system\].*/d" Jul 15 11:31:08 gateway-laptop dbus[3076]: [system] Successfully activated service 'org.freedesktop.PackageKit' ``` Which I would have thought should match. Reducing this complexity, this doesn't match ``` $ echo "Jul 15 11:31:08 gateway-laptop dbus[3076]: [system] Successfully activated service 'org.freedesktop.PackageKit'"|sed "/^\w{3}.*"/d Jul 15 11:31:08 gateway-laptop dbus[3076]: [system] Successfully activated service 'org.freedesktop.PackageKit' ``` It looks like its behavior ^\w{3} should match the first 3 alphanumeric characters in the line, then the .* should match the rest of the line to the EOL.