我有一个正则表达式,它正在按预期工作: https://regex101.com/r/fR4mI1/1
它找到所有整数1-9,但不是零,或者更准确地说,它找到非非数字也不是0.这正是我在下面发生的事情,但它不是那样的。我的输入中有0个以奇怪的方式出现在我的输出中。我正在努力做的其他事情都很好,但我根本不需要任何零。我做错了什么?
#!/bin/bash
#not non-digits or 0, replace with capture and new line
#due to confusion, yes I've tried [1-9] same result
#I understand the statement is not intuitive, that's why I clarified in my explanation
nums=$(echo 821312010476 | sed -r 's/([^\D0])/\1\n/g')
declare -a array
#make array
#error
# 01 and 04 from string above return as 01 and 04 and not 1 or 4
#why are there 0's still?
for num in $nums; do
echo $num
array+=($num)
done
for ((index= 0; index <=${#array[@]}; index++)); do
#make string of 2 integer chars to see if value is within range
myTempNum="${array[index]}${array[index+1]}"
if [[ myTempNum -gt 0 && myTempNum -lt 30 ]]
then
echo $myTempNum
index=$index+1
elif [[ ${array[index]} -gt 0 ]]
then
echo ${array[index]}
fi
done
示例输出
8
2
1
3
1
2
01
04
7
6
8
21
3
12
01
04
7
6
你可以在这里看到01和04两次
答案 0 :(得分:0)
nums=$(echo 821312010476 | sed -r 's/([^\D0])/\1\n/g; s/0/''/g')
这是解决方案,sed没有取代零。谢谢anubhava