-3

I want to take the first 4 numbers of the string phone numbers

example

phoneNumber := "081911254966"

I want to get from regex Go and get the result "0819"

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189

1 Answers1

2
package main

import (
    "fmt"
    "regexp"
)

func main() {
    phoneNumber := "081911254966"
    r := regexp.MustCompile(`^\d{4}`).FindString(phoneNumber)
    r2 := regexp.MustCompile(`\d{4}`).FindString("trash text 081911254966 trash text")
    // if not need regexp
    fmt.Println(phoneNumber[:4])
    fmt.Println(r)
    fmt.Println(r2)
}

0819
0819
0819


Genius Merely
  • 374
  • 2
  • 11