Go (RE2)

Decimal Number in GO

Matches decimal numbers, including integers and negatives.

Try it in the GO tester →

Pattern

regexGO
^-?\d+(\.\d+)?$

Go (RE2) code

goGo
package main

import (
	"fmt"
	"regexp"
)

func main() {
	re := regexp.MustCompile(`^-?\d+(\.\d+)?$`)
	input := `3.14`
	for _, match := range re.FindAllString(input, -1) {
		fmt.Println(match)
	}
}

Uses `regexp.MustCompile` (panics on bad patterns at startup) and `FindAllString` for all matches.

How the pattern works

`^-?\d+` matches optional sign and integer part. `(\.\d+)?` optionally matches a decimal point and fractional digits.

Examples

Input

3.14

Matches

  • 3.14

Input

-0.001

Matches

  • -0.001

Input

42

Matches

  • 42

Same pattern, other engines

← Back to Decimal Number overview (all engines)