可能重复:
Need a Regular expression to extract 5th to 8th charactors in a string.UNIX正则表达式从字符串基于索引位置提取字段
我有用于例如数值的字符串:14150712.M;我需要从索引位置5到8提取数字。例如:0712如何从FileName中删除Ist四个数字,然后切下下四个数字?
可能重复:
Need a Regular expression to extract 5th to 8th charactors in a string.UNIX正则表达式从字符串基于索引位置提取字段
我有用于例如数值的字符串:14150712.M;我需要从索引位置5到8提取数字。例如:0712如何从FileName中删除Ist四个数字,然后切下下四个数字?
试试这个正则表达式:
/\d{4}(\d{4})/
所需数据将在内部匹配的括号
echo "14150712.M" | cut -c5-8
如果位置是固定的,你可以使用bash的字符串参数扩展:
A="ABCDE"; echo ${A:1:2}
这将打印BC
。您可以在for A in *; do
循环使用,例如:
for A in *.M; do
NUMID=${A:4:4}
echo "We extracted \"${NUMID}\" from \"${A}\"."
done
什么[正则表达式的味道(grep的,PERL,AWK ,sed,vim,posix re)](http://www.regular-expressions.info/index.html)? – sehe
这是从http://stackoverflow.com/questions/6661085/need-a-regular-expression-to-extract-5th-to-8th-charactors-in-a-string? – Kobi