0

I'm trying to find every number where more than two individual digits double. Such as:

  • 0000000012
  • 1837488809

My code is:

if [[ $number =~ (.)\1{3,10} ]];
then
    echo "$number found"
fi

It is working but not as expected: It echo’s out numbers containing on 11, and just them.

What am I doing wrong?

7
  • what do you mean containing on 11?, also two double numbers means it should match 1122 right? Commented Sep 4, 2015 at 18:32
  • 1122 should not appear. i made my question a bit more precise, hopefully. Commented Sep 4, 2015 at 18:39
  • 1
    What's your input? I think you're looking for (\d)\1{2,}. Commented Sep 4, 2015 at 18:46
  • Add set -x to your script and see what the [[ command is actually seeing. Commented Sep 4, 2015 at 18:47
  • The upper bound on the brace match isn't necessary. If the lower bound matches it doesn't matter how many more there are. Commented Sep 4, 2015 at 18:48

3 Answers 3

1

I propose GNU grep:

grep -oE '(.)(\1){2,9}' <<< "1837488809"

Output:

888
Sign up to request clarification or add additional context in comments.

Comments

0

I went with:

check="grep -oE '(.)(\1){2,9}'"
if [ $check -gt 1 ]; then ...

Comments

0

bash regular expressions don't support back references, so you are simply looking for a string containing at least one character before a string of 3-10 1s. A correct regular expression would be

# Tedious, yes, but not really any longer than a piece of code to generate it
regex="0{3,10}|1{3,10}|2{3,10}|3{3,10}|4{3,10}|5{3,10}|6{3,10}|7{3,10}|8{3,10}|9{3,10}"
if [[ $number =~ $regex ]];
then
    echo "$number found"
fi

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.