Golang | Finding Index of the Regular Expression present in Slice

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 find the leftmost index value of the specified regular expression in the given slice of bytes with the help of FindIndex() method. This method returns a two-element slice of integers which defines the location of the leftmost match in the given slice of the regular expression and the match like s[loc[0]:loc[1]]. Or it will return nil if no match found. This method is defined under the regexp package, so for accessing this method you need to import the regexp package in your program.

Syntax:

func (re *Regexp) FindIndex(s []byte) (loc []int)

Example 1:



filter_none

edit
close

play_arrow

link
brightness_4
code

// Go program to illustrate how to find the index
// value of the regexp in the given slice
  
package main
  
import (
    "fmt"
    "regexp"
)
  
// Main function
func main() {
  
    // Finding the index value of regexp 
    // from the given slice of bytes
    // Using FindIndex() method
    m := regexp.MustCompile(`ek`)
  
    fmt.Println(m.FindIndex([]byte(`GeeksgeeksGeeks, geeks`)))
    fmt.Println(m.FindIndex([]byte(`Hello! geeksForGEEKs`)))
    fmt.Println(m.FindIndex([]byte(`I like Go language`)))
    fmt.Println(m.FindIndex([]byte(`Hello, Welcome`)))
  
}

chevron_right


Output:

[2 4]
[9 11]
[]
[]

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Go program to illustrate how to find the
// index value of the regexp in the given slice
package main
  
import (
    "fmt"
    "regexp"
)
  
// Main function
func main() {
  
    // Finding regexp from
    // the given slice
    // Using Find() method
    m := regexp.MustCompile(`45`)
    res := m.Find([]byte(`I45, like345, Go-234 langu34age`))
  
    if res == nil {
        fmt.Println("Nil found")
    } else {
  
        // Finding the index value of
        // the regexp from the given slice
        // Using FindIndex() method
        r := m.FindIndex([]byte(`I45, like345, Go-234 langu34age`))
        fmt.Printf("Found: %q with index value: %d", res, r)
    }
}

chevron_right


Output:

Found: "45" with index value: [1 3]


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.