Finding Index of the Regular Expression present in String of Golang


Regular expressions are an important tool in Golang for searching and manipulating strings. They allow you to search for patterns in text and extract information based on those patterns. One common task is finding the index of a regular expression in a string. In this article, we will explore how to find the index of a regular expression in a string in Golang.

Finding Index of Regular Expression Present in String in Golang

In Golang, we can find the index of a regular expression present in a string using the "regexp" package. The "regexp" package provides a "FindStringIndex" function that returns the starting and ending indices of the first match of the regular expression in the string.

Example 1: Finding the index of the first occurrence of a regular expression in a string

package main

import (
   "fmt"
   "regexp"
)

func main() {
   str := "The quick brown fox jumps over the lazy dog"
   re := regexp.MustCompile(`fox`)
   index := re.FindStringIndex(str)
   fmt.Println(index) // [16 19]
}

Output

[16 19]

In the above code, we are finding the index of the first occurrence of the regular expression fox in the given string The quick brown fox jumps over the lazy dog. The FindStringIndex() function of the regexp package returns a slice of two integers representing the start and end positions of the first occurrence of the regular expression in the string. In this case, the regular expression fox occurs at positions 16 to 18 in the string.

Example 2: Finding the index of all occurrences of a regular expression in a string

package main

import (
   "fmt"
   "regexp"
)

func main() {
   str := "The quick brown fox jumps over the lazy dog"
   re := regexp.MustCompile(`o.`)
   indices := re.FindAllStringIndex(str, -1)
   fmt.Println(indices) // [[14 16] [17 19] [24 26] [27 29]]
}

Output

[[12 14] [17 19] [26 28] [41 43]]

In this code, we are finding the index of all occurrences of the regular expression o. in the given string The quick brown fox jumps over the lazy dog. The FindAllStringIndex() function of the regexp package returns a slice of slices, where each slice contains the start and end positions of an occurrence of the regular expression in the string. In this case, the regular expression o. occurs at positions 14 to 15, 17 to 18, 24 to 25, and 27 to 28 in the string.

Example 3: Handling non-matching regular expressions

package main

import (
   "fmt"
   "regexp"
)

func main() {
   str := "The quick brown fox jumps over the lazy dog"
   re := regexp.MustCompile(`cat`)
   index := re.FindStringIndex(str)
   if index == nil {
      fmt.Println("Regular expression not found in string")
   } else {
      fmt.Println(index)
   }
}

Output

Regular expression not found in string

Conclusion

Finding the index of a regular expression present in a string is a common task in Golang. The "regexp" package provides a simple and easy-to-use solution for this task. With the help of the "FindStringIndex" function, we can easily find the starting and ending indices of the first match of the regular expression in the string.

Updated on: 17-Apr-2023

774 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements