Golang | Checking the string for the specified regular expression

A regular expression is a sequence of characters which define a search pattern. Go language support regular expressions. A regular expression is used for parsing, filtering, validating, and extracting meaningful information from large text, like logs, the output generated from other programs, etc.
In Go regexp, you are allowed to check whether the given string contains any match of the specified regular expression pattern with the help of MatchString() function. This function is defined under the regexp package, so for accessing this method you need to import the regexp package in your program.

Syntax:

func MatchString(pattern string, s string) (matched bool, err error)

Here, p represents the pattern and str represents a string. This function returns true if the pattern matched or return false if the pattern does not match. And also return an error if found.



Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Go program to illustrate how to check the 
// given regexp present in the given string
package main
  
import (
    "fmt"
    "regexp"
)
  
// Main function
func main() {
  
    // Creating and initializing string
    // Using shorthand declaration
    s1 := "Welcome to GeeksforGeeks"
    s2 := "I like Go language!!!"
  
    // Pattern
    p1 := "to"
    p2 := "ang"
    p3 := "OO"
    p4 := "!"
  
    // Matching pattern
    // Using MatchString() function
    res1, e := regexp.MatchString(p1, s1)
    fmt.Println("Result and Error is:", res1, e)
  
    res2, e := regexp.MatchString(p2, s1)
    fmt.Println("Result and Error is:", res2, e)
  
    res3, e := regexp.MatchString(p3, s1)
    fmt.Println("Result and Error is:", res3, e)
  
    res4, e := regexp.MatchString(p4, s1)
    fmt.Println("Result and Error is:", res4, e)
  
    res5, e := regexp.MatchString(p1, s2)
    fmt.Println("Result and Error is:", res5, e)
  
    res6, e := regexp.MatchString(p2, s2)
    fmt.Println("Result and Error is:", res6, e)
  
    res7, e := regexp.MatchString(p3, s2)
    fmt.Println("Result and Error is:", res7, e)
  
    res8, e := regexp.MatchString(p4, s2)
    fmt.Println("Result and Error is:", res8, e)
  
}

chevron_right


Output:

Result and Error is: true <nil>
Result and Error is: false <nil>
Result and Error is: false <nil>
Result and Error is: false <nil>
Result and Error is: false <nil>
Result and Error is: true <nil>
Result and Error is: false <nil>
Result and Error is: true <nil>

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Go program to illustrate how to check
// the given regexp present in the string
package main
  
import (
    "fmt"
    "regexp"
)
  
// Main function
func main() {
  
    // Matching pattern in the 
    // given string using
    // MatchString() function
    res1, e := regexp.MatchString(`eks`, "GeeksforGeeks")
    fmt.Println(res1, e)
  
    res2, e := regexp.MatchString(`BAN`, "Banana")
    fmt.Println(res2, e)
  
    res3, e := regexp.MatchString(`123`, "Welcome to GeeksforGeeks")
    fmt.Println(res3, e)
  
    res4, e := regexp.MatchString(`e(ks`, "GFG")
    fmt.Println(res4, e)
  
}

chevron_right


Output:

true <nil>
false <nil>
false <nil>
false error parsing regexp: missing closing ): `e(ks`


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.