Difference Between [0-9]* and [0-9] [Duplicate]

I am learning regexes and found some codes so I tested in my terminal and got results given below

$ echo "my version 3.8.0" | grep -o '[0-9.]*'
3.8.0
$ echo "my version 3.8.0" | grep -o '[0-9]*'
3
8
0
$ echo "my version 3.8.0" | grep -o '[0-9]'
3
8
0

Why last two expressions are giving same output so I want to know how ? And one more thing , the first expression output is in one line where rest two expressions output is in multiple lines why ? I am new to regexes and its very confusing

Finally I am just want to know the working flow of above expressions or line of code

2

First, quick note:

[0-9] matches exactly one digit
[0-9]* matches a sequence of digits of any length

In your case, the largest sequence of digits in 3.8.0 is either 3 or 8 or 0 as they're separated by .

If your version were 38.1.0, for instance, then you would see a difference:

[0-9] would give 3, 8, 1, 0
[0-9]* would give 38, 1, 0
0
[0-9.]*

matches zero or more characters from the set 0123456789.. It can therefore match the entire string 3.8.0.

[0-9]*

matches zero or more digits. Therefore it can match each of the digits but has to omit the dots. You get three matches; one is displayed per line.

[0-9]

matches exactly one digit. Since 3.8.0 only contains single digits, the output of the last two regexes is identical. That would change with inputs like 3.8.10.

The * at the end of any range(or character) makes it count all occurrences matching the range(character) zero or more number of times.

For example, [0-9]* on 38.65.32 will give you

38
65
32

But, [0-9] on the same will give you

3
8
6
5
3
2

[0-9.]* will give you 3.8.0 because you use a character class which will match either a digit or a dot zero or more times matching all the characters.

[0-9]* Will match zero or more times a digit using a quantifier. Since the example data contains single digits separated by a dot, not more than a single digit can be matched because the dot will not be matched.

[0-9] Will match a single digit without a quantifier.

If for example your string was "my version 3.80.0":

[0-9.]* Would match 3.80.0

[0-9]* Would match 3 80 0

[0-9] Would match 3 8 0 0

1
Marcus Vance

Marcus Vance

Cybersecurity & Digital Privacy Researcher

Marcus Vance is a cybersecurity auditor and technology writer dedicated to educating the public about online safety, data privacy regulations, enterprise security, and emerging cyber threats.

Share this article
Twitter Facebook Pinterest