-3

I have a regex, and it is working as intended here: https://regex101.com/r/fR4mI1/1

It finds all integers 1-9, but not zero, or more accurately, it finds Not non digits nor 0. That is exactly what I have going on below, but it isn't working out that way. I have 0's from my input showing up in my output in strange ways. Everything else I'm trying to do is working fine, but I don't want any zeros at all. What am I doing wrong?

   #!/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

example output

8
2
1
3
1
2
01
04
7
6
8
21
3
12
01
04
7
6

you can see the 01 and 04 here both times

nanthil
  • 65
  • 1
  • 4
  • 11
  • Because I tried that and was having the same problem, so I tried another solution: http://stackoverflow.com/questions/16999328/regex-pattern-that-matches-any-number-include-1-9-except-2 I didn't express it in a confusing way, the regex might be confusing for you but I was rather articulate. I have zeroes popping up in my output when they shouldn't be. My question can't be more plainly stated. – nanthil Apr 07 '16 at 07:12
  • The regex does not remove `0`s. Try `'s/0*([^\D0])/\1\n/g'` or just `'s/0*([1-9])/\1\n/g'` – Wiktor Stribiżew Apr 07 '16 at 07:17
  • 1
    Why `0` shouldn't be there as your regex is `[^\D0]` which means don't replace `0s` – anubhava Apr 07 '16 at 07:17
  • OH! so it's sed not replacing them, because the regex does not find them! I see, didn't think about it from that way. – nanthil Apr 07 '16 at 07:24

1 Answers1

0
nums=$(echo 821312010476 | sed -r 's/([^\D0])/\1\n/g; s/0/''/g')

this was the solution, sed was not replacing the zeros. thanks anubhava

nanthil
  • 65
  • 1
  • 4
  • 11