unix - Using regex to extract a substring while excluding a certain phrase -


say string:

test.1234.mp4  

i extract numbers 1234 without extracting 4 in mp4

what regex this?

the numbers aren't in second position , can in different positions , might not 4 digits. extract number without extracting 4 in mp4 essentially.

more examples:

test.abc.1234.mp4 test.456.abc.mp4 test.aaa.bbb.c.111.mp4 test.e666.123.mp4 

essentially numbers extracted. hence, last example, 666 e666 not extracte , 123. extract have been using

echo "example.123.mp4" | grep -o "regex" 

edit: test456 meant test.456

the accepted answer fail on "test.e666.123.mp4" (print 666).

this should work

$ cat | perl -ne '/\.(\d+)\./; print "$1\n"' test.abc.1234.mp4 test.456.abc.mp4 test.aaa.bbb.c.111.mp4 test.e666.123.mp4 1234 456 111 123 

note print first group of numbers, if have test.123.456.mp4 123 printed.

the idea match dot followed numbers interested in (parentheses save match), followed dot. means fail on 123.mp4.

to fix have:

$ cat | perl -ne '/(^|\.)(\d+)\./; print "$2\n"' test.abc.1234.mp4 test.456.abc.mp4 test.aaa.bbb.c.111.mp4 test.e666.123.mp4 781.test.mp4 1234 456 111 123 781 

first match either beginning of line (^) or dot, followed numbers , dot. use $2 here since $1 either beginning of line or dot.


Comments

Popular posts from this blog

c# - Operator '==' incompatible with operand types 'Guid' and 'Guid' using DynamicExpression.ParseLambda<T, bool> -