An overview of the

You can match numbers with d in Golang. In fact, \d can be used to match the entire range.

09 -
Copy the code

The regular expression that matches any number will be

\d
Copy the code

If you only want to match a specific number, say 5, then the regular expression will be that number.

5
Copy the code

If you want to match two numbers, the following will be the regular expression

\d\d
Copy the code

Match a number

Let’s look at an example

package main

import (
	"fmt"
	"regexp"
)

func main(a) {
	sampleRegexp := regexp.MustCompile(`\d`)

	fmt.Println("For regex \\d")
	match := sampleRegexp.MatchString("1")
	fmt.Printf("For 1: %t\n", match)

	match = sampleRegexp.MatchString("4")
	fmt.Printf("For 4: %t\n", match)

	match = sampleRegexp.MatchString("9")
	fmt.Printf("For 9: %t\n", match)

	match = sampleRegexp.MatchString("a")
	fmt.Printf("For a: %t\n", match)

	sampleRegexp = regexp.MustCompile(5 ` `)
	fmt.Println("\nFor regex 5")
	match = sampleRegexp.MatchString("5")
	fmt.Printf("For 5: %t\n", match)

	match = sampleRegexp.MatchString("6")
	fmt.Printf("For 6: %t\n", match)
}
Copy the code

The output

For regex \d
For 1: true
For 4: true
For 9: true
For a: false

For regex 5
For 5: true
For 6: false
Copy the code

In the above program, we have two examples of regular expressions

  • D – matches any number

  • 5- Matches only 5

The first one matches any single number. That’s why it matches, right

1
4
9
Copy the code

And it doesn’t match

a
Copy the code

You can see this in the output as well

The second phrase matches only **”5″, not “6”, as you can see from the ** output.

For regex 5
For 5: true
For 6: false
Copy the code

Matches duplicate numbers

Quantifiers can be used to match the repetition of numbers. For example,

  • \d+ – Matches one or more numbers

  • \*- Matches zero or more digits

  • D {N}- Matches N numbers

package main

import (
	"fmt"
	"regexp"
)

func main(a) {
	sampleRegexp := regexp.MustCompile(`\d+`)

	fmt.Println(`For regex \d+`)
	match := sampleRegexp.MatchString("12345")
	fmt.Printf("For 12345: %t\n", match)

	match = sampleRegexp.MatchString("")
	fmt.Printf("For empty string: %t\n", match)

	sampleRegexp = regexp.MustCompile(`\d*`)

	fmt.Println()
	fmt.Println(`For regex \d*`)
	match = sampleRegexp.MatchString("12345")
	fmt.Printf("For 12345: %t\n", match)

	match = sampleRegexp.MatchString("")
	fmt.Printf("For empty string: %t\n", match)

	sampleRegexp = regexp.MustCompile(`\d{2}`)

	fmt.Println()
	fmt.Println(`For regex \d{2}`)
	match = sampleRegexp.MatchString("12")
	fmt.Printf("For 12: %t\n", match)

	match = sampleRegexp.MatchString("1")
	fmt.Printf("For 1: %t\n", match)
}
Copy the code

The output

For regex \d+
For 12345: true
For empty string: false

For regex \d*
For 12345: true
For empty string: true

For regex \d{2}
For 12: true
For 1: false
Copy the code

In the program above, we have three examples of phrases

  • \d+

  • \d*

  • d\{N}

\d+ regex gives a matching **”12345″, but ** fails for an empty string.

**\d* matches “12345 “** and an empty string

D {2} matches a sequence of two numbers. That’s why it matches “12 “and not “1”.

Also, check out our Golang advanced Tutorial series at ——. Golang Advanced tutorial

Golang Regex: Matching numbers or numerical values in regular expressions appears on Welcome To Golang By Example.