DALLASCAO.COM

Site of Cao Shouguang, English to Chinese translator

sed regular expressions

echo 'iweigh297lbs' | sed 's/.*[^0-9]\([0-9]\+\).*/\1/'

The expression has to match the whole line.
Greed: The expression will match the largest possible from left to right.
This won’t work.

echo 'iweigh297lbs' | sed 's/.*\([0-9]\+\).*/\1/'

Output 7

This won’t work

echo 'iweigh297lbs' | sed 's/.*[^0-9]\([0-9]*\).*/\1/'

This works

echo 'iweigh297lbs' | sed 's/.*[^0-9]\([0-9][0-9]*\).*/\1/'

+ has to be escaped. * should not be escaped.

Leave a Comment