Scientific Notation in GO
Matches numbers in scientific/exponential notation (e.g., 1.5e10).
Try it in the GO tester →Pattern
regexGO
^-?\d+(\.\d+)?[eE][+-]?\d+$Go (RE2) code
goGo
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`^-?\d+(\.\d+)?[eE][+-]?\d+$`)
input := `1.5e10`
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+(\.\d+)?` matches a signed decimal. `[eE]` allows either exponent marker. `[+-]?\d+$` matches the signed exponent digits.
Examples
Input
1.5e10Matches
1.5e10
Input
-2.7E-5Matches
-2.7E-5
Input
3.14No match
—