regex - egrep "[\w]+" doesnt match a word -


in linux, when try simple egrep word doesnt match. following doesnt return result, expect sample in both cases match:

echo "sample"|egrep "[\w]+" echo "sample"|egrep "[\w]" 

however following returns word sample :

echo "sample"|egrep "[\w]*" sample 

with extended matching, entities \w not expanded inside square brackets. therefore avoid them:

echo sample | grep -e '\w+' 

(egrep deprecated alias grep -e.)


if want use square brackets, use perl regular expressions:

echo sample | grep -p '[\w]+' 

(note though gnu-specific, non-posix feature.)


finally, interest, [\w]* -e not match word, matches 0 or more characters. , given @ least 0 characters contained in every string, grep shows every line. compare:

echo another.sample | grep --color=always -e '[\w]*' echo another.sample | grep --color=always -e '\w+' 

you'll see first command returns black string (meaning has not matched character). second 1 instead highlight another , sample, not dot, meaning has correctly matched 2 words. may want try grep -eo.


Comments

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -

python - How to remove the Xframe Options header in django? -