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:
Output:
[2 4]
[9 11]
[]
[]
Example 2:
Output:
Found: "45" with index value: [1 3]