Menu Close

regexp package in Go

Here, we will are going to learn about regexp package in Go with deep understanding and example in details.

The regexp package in Go offers rich functionalities to work with regular expressions. Regular expressions or regex, are sequences of characters forming a search pattern, primarily used for string pattern matching and manipulation.

Example:

package main


import (

	"fmt"

	"regexp"

)


func main() {

	email := "[email protected]"

	matched, err := regexp.MatchString(`^[a-z0-9._%+\-]+@[a-z0-9.\-]+\.[a-z]{2,4}$`, email)


	if err != nil {

		fmt.Printf("Error occurred: %v", err)

		return

	}


	if matched {

		fmt.Println("Email is valid.")

	} else {

		fmt.Println("Email is not valid.")

	}

}

In this example, regexp.MatchString is a function that checks if the string (in this case, an email address) matches the regular expression pattern. The regular expression used is a standard pattern for validating an email address. It ensures that the string contains an @ symbol, followed by letters, numbers, hyphens, or periods, and ends with a domain that is 2 to 4 characters long. If the email matches the pattern, “Email is valid” is printed. Otherwise, “Email is not valid” is displayed.

regexp functions

  • Match
  • MatchReader

To learn more about golang, Please refer given below link.

https://www.techieindoor.com/golang-tutorial/

References:

https://golang.org/doc/

Posted in golang, packages, regexp

Leave a Reply

Your email address will not be published. Required fields are marked *