How to look for a numeral, a sequence of numerals or number ? (Part 2 )

We will start doing basic things, then combine those basics to achieve advanced pattern search using Regex.

This tutorial will be about searching for numerals and numbers.

  1. How to look for a simple numeral

We consider the sample text in the text editor. We want to look for all numerals in this text. For this, we use the pattern [0-9]. It means look for any numeral in the range (0,9), Note that 0 and 9 are included. Notice that Sublime found a total of 13 matches. Look at the bottom left of the Sublime text window to know the number of matches

2. How to look for a sequence of numerals, or a number :

We use the pattern [0-9]*. It tells Sublime to search for a repeated occurence of a numeral in the range (0,9).

Notice one thing, the number of matches is 338. But we are seeing only 5 numbers. What is happening here ?

The thing is that the asterisk * looks for a sequence of count ZERO or more times. which means that empty sequences are also considered VALID matches. which means all characters and spaces and special characters are also counted. But that's not what we want !

Fair enough, we want only plain number sequences. For this aim, we will use the pattern [0-9]+. The character + means ONE or more occurences. Demo in the next screen capture :

Now we have only 5 numbers as matches. Which is exaclty what we want.

Now we can use the range pattern to do more interesting things. Let's say we want to look for numbers bigger than 30 and smaller than 99. We can use the pattern [3-9][0-9]

Notice that 20 is not included.

Key takeaways :

  1. [0-9] looks for an occurence of a numeral in the range (0,9)

  2. * means a repeated occurence of ZERO or more times

  3. + means a repeated occurence of ONE or more times

In the next tutorial, we will study the regex patterns specific to characters.