Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

As the title suggests, which one is better to match numbers, [[:digit:]] or [0-9]?

I'm using the bash shell

Thanks :)

share|improve this question
    
What do you mean by "better" ? if both achieve the same goal, both are good enough solution unless you have extra requirements you haven't mentioned. –  Stephane Jun 28 '10 at 15:07
    
hmm I'm thinking about performance, readability and portability, even though the first has been already answered :) –  Likso Jun 28 '10 at 15:58
add comment

2 Answers

up vote 5 down vote accepted

The only reason that [[:digit:]] must be used is to support locales that use digits other than 0-9. For example Arabic-Indic Numerals: ٠١٢٣٤٥٦٧٨٩ (Unicode U+0660 through U+0669). Otherwise for the Hindu-Arabic numerals 0123456789, [0-9] works equally as well as [[:digit:]].

share|improve this answer
add comment
# time grep -oE '[[:digit:]]' /etc/services
...
real    0m0.029s
user    0m0.017s
sys     0m0.013s


# time grep -oE '[0-9]' /etc/services
...
real    0m0.029s
user    0m0.016s
sys     0m0.012s

I could probably write a quick script to average them, and I bet I'd find that the averages are identical, but it certainly gives you the idea.

share|improve this answer
2  
gist.github.com/456559 –  Andrew Jun 28 '10 at 23:46
    
+1, I was so focused on summing/averaging that it didn't occur to me to just time the whole big glob. Well-played. –  BMDan Jun 30 '10 at 13:27
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.