Skip to main content

Command Palette

Search for a command to run...

finding the substring in text by the regex pattern

Published
2 min readView as Markdown
S
DevRel at StackGen | Formerly at Deepfence ,Tenable , Accurics | AWS Community Builder also Docker Community Award Winner at Dockercon2020 | CyberSecurity Innovator of Year 2023 award by Bsides Bangalore | Docker/HashiCorp Meetup Organiser Bangalore & Co-Author of Learn Lightweight Kubernetes with k3s (2019) , Packt Publication & also run Non Profit CloudNativeFolks / CloudSecCorner Community To Empower Free Education reach out me twitterhttps://twitter.com/sangamtwts or just follow on GitHub -> https://github.com/sangam14 for Valuable Resources
  • There are always tasks such as validating the input, searching the document for any information, or even cleaning up a given string from unwanted escape characters. For these cases, regular expressions are usually used.

  • The Go standard library contains the regexp package, which covers the operations with regular expressions.

Create the main.go file with the following content:

package main

import (
    "fmt"
    "regexp"
)

const refString = `[{ \"email\": \"email@example.com\" \"phone\": 555467890},
{ \"email\": \"other@domain.com\" \"phone\": 555467890}]`

func main() {

    // This pattern is simplified for brevity
    emailRegexp := regexp.MustCompile("[a-zA-Z0-9]{1,}@[a-zA-Z0-9]{1,}\\.[a-z]{1,}")
    first := emailRegexp.FindString(refString)
    fmt.Println("First: ")
    fmt.Println(first)

    all := emailRegexp.FindAllString(refString, -1)
    fmt.Println("All: ")
    for _, val := range all {
        fmt.Println(val)
    }

}

output:-

sangam:golang-daily sangam$ go run main.go
First: 
email@example.com
All: 
email@example.com
other@domain.com
sangam:golang-daily sangam$

How it works...

  • The FindString or FindAllString functions are the simplest ways to find the matching pattern in the given string. The only difference is that the FindString method of Regexp will return only the first occurrence. On the other hand, the FindAllString, as the name suggests, returns a slice of strings with all occurrences.

  • The Regexp type offers a rich set of FindXXX methods. This recipe describes only the String variations that are usually most useful. Note that the preceding code uses the MustCompile function of the regexp package, which panics if the compilation of the regular expression fails.

More from this blog

C

CloudNativeFolks Community

140 posts

CloudNativeFolks is non profit community that empower and educate about cloud native technology !