The Wayback Machine - https://web.archive.org/web/20240829143053/https://www.geeksforgeeks.org/golang-finding-index-of-the-regular-expression-present-in-string/
Open In App

Golang | Finding Index of the Regular Expression present in String

Last Updated : 05 Sep, 2019
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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 string with the help of the FindStringIndex() method. This method returns a two-element slice of integers which defines the location of the leftmost match in the given string of the regular expression and the match like str[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) FindStringIndex(str string) (loc []int)

Example 1:




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


Output:

[1 3]
[8 10]
[]
[]

Example 2:




// Go program to illustrate how to find the index
// value of the regexp in the given string
package main
  
import (
    "fmt"
    "regexp"
)
  
// Main function
func main() {
  
    // Finding the regexp 
    // from the given string
    // Using Find() method
    m := regexp.MustCompile(`345`)
    res := m.FindString("I45, like345, Go-234 langu34age")
  
    // Finding the index value of regexp in the given string
    // UsingFindStringIndex() method
    r := m.FindStringIndex("I45, like345, Go-234 langu34age")
    fmt.Printf("Found: %s with index value: %d", res, r)
}


Output:

Found: 345 with index value: [9 12]


Similar Reads

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
2 min read
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
3 min read
Golang | Extracting a Regular Expression from the String
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 extract a regular
2 min read
Golang | Extracting all the Regular Expression from the String
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 all the regul
2 min read
Golang | Replacing all String which Matches with 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 replace original s
2 min read
Golang | Checking the byte of slice for 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
3 min read
Golang | Extracting Regular Expression from the 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 a regular exp
2 min read
Finding the index value of specified byte in slice of bytes in Golang
In Go language slice is more powerful, flexible, convenient than an array, and is a lightweight data structure. The slice is a variable-length sequence which stores elements of a similar type, you are not allowed to store different type of elements in the same slice. In the Go slice of bytes, you can find the first index value of the specified byte
3 min read
Check if the given characters is present in Golang String
In Go language, strings are different from other languages like Java, C++, Python, etc. It is a sequence of variable-width characters where each and every character is represented by one or more bytes using UTF-8 Encoding. In Go strings, you are allowed to check the given characters present in the string using the given functions. These functions a
3 min read
Finding the Hyperbolic Tangent of Complex Number in Golang
Go language provides inbuilt support for basic constants and mathematical functions for complex numbers with the help of the cmplx package. You are allowed to find the hyperbolic tangent of the specified complex number with the help of Tanh() function provided by the math/cmplx package. So, you need to add a math/cmplx package in your program with
2 min read
Finding the Hyperbolic Sine of Complex Number in Golang
Go language provides inbuilt support for basic constants and mathematical functions for complex numbers with the help of the cmplx package. You are allowed to find the hyperbolic sine of the specified complex number with the help of Sinh() function provided by the math/cmplx package. So, you need to add a math/cmplx package in your program with the
2 min read
Finding Absolute Value For Specified Complex Number in Golang?
Go language provides inbuilt support for basic constants and mathematical functions for complex numbers with the help of the cmplx package. You are allowed to find the absolute value of the specified complex number with the help of Abs() function provided by the math/cmplx package. So, you need to add a math/cmplx package in your program with the h
2 min read
Finding the Inverse Cosine of Complex Number in Golang
Go language provides inbuilt support for basic constants and mathematical functions for complex numbers with the help of the cmplx package. You are allowed to find the inverse cosine of the specified complex number with the help of Acos() function provided by the math/cmplx package. So, you need to add a math/cmplx package in your program with the
2 min read
Finding the Inverse Hyperbolic Sine of Complex Number in Golang
Go language provides inbuilt support for basic constants and mathematical functions for complex numbers with the help of the cmplx package. You are allowed to find the inverse hyperbolic sine of the specified complex number with the help of Asinh() function provided by the math/cmplx package. So, you need to add a math/cmplx package in your program
2 min read
Finding the Inverse Hyperbolic Cosine of Complex Number in Golang
Go language provides inbuilt support for basic constants and mathematical functions for complex numbers with the help of the cmplx package. You are allowed to find the inverse hyperbolic cosine of the specified complex number with the help of Acosh() function provided by the math/cmplx package. So, you need to add a math/cmplx package in your progr
2 min read
Finding the Inverse Hyperbolic Tangent of Complex Number in Golang
Go language provides inbuilt support for basic constants and mathematical functions for complex numbers with the help of the cmplx package. You are allowed to find the inverse hyperbolic tangent of the specified complex number with the help of the Atanh() function provided by the math/cmplx package. So, you need to add a math/cmplx package in your
2 min read
Finding the Inverse Tangent of Complex Number in Golang
Go language provides inbuilt support for basic constants and mathematical functions for complex numbers with the help of the cmplx package. You are allowed to find the inverse tangent of the specified complex number with the help of the Atan() function provided by the math/cmplx package. So, you need to add a math/cmplx package in your program with
2 min read
Finding the Inverse Sine of Complex Number in Golang
Go language provides inbuilt support for basic constants and mathematical functions for complex numbers with the help of the cmplx package. You are allowed to find the inverse sine of the specified complex number with the help of the Asin() function provided by the math/cmplx package. So, you need to add a math/cmplx package in your program with th
2 min read
Finding the Cosine of Complex Number in Golang
Go language provides inbuilt support for basic constants and mathematical functions for complex numbers with the help of the cmplx package. You are allowed to find the cosine of the specified complex number with the help of the Cos() function provided by the math/cmplx package. So, you need to add a math/cmplx package in your program with the help
2 min read
Finding the Sine of Complex Number in Golang
Go language provides inbuilt support for basic constants and mathematical functions for complex numbers with the help of the cmplx package. You are allowed to find the sine of the specified complex number with the help of the Sin() function provided by the math/cmplx package. So, you need to add a math/cmplx package in your program with the help of
2 min read
Finding the Tangent of Complex Number in Golang
Go language provides inbuilt support for basic constants and mathematical functions for complex numbers with the help of the cmplx package. You are allowed to find the tangent of the specified complex number with the help of the Tan() function provided by the math/cmplx package. So, you need to add a math/cmplx package in your program with the help
2 min read
Finding the Cotangent of Complex Number in Golang
Go language provides inbuilt support for basic constants and mathematical functions for complex numbers with the help of the cmplx package. You are allowed to find the cotangent of the specified complex number with the help of the Cot() function provided by the math/cmplx package. So, you need to add a math/cmplx package in your program with the he
2 min read
Finding the Decimal Logarithm of Complex Number in Golang
Go language provides inbuilt support for basic constants and mathematical functions for complex numbers with the help of the cmplx package. You are allowed to find the decimal logarithm of the specified complex number with the help of Log10() function provided by the math/cmplx package. So, you need to add a math/cmplx package in your program with
2 min read
Finding the Square Root of the Complex Number in Golang
Go language provides inbuilt support for basic constants and mathematical functions for complex numbers with the help of the cmplx package. You are allowed to find the square root of the specified complex number with the help of Sqrt() function provided by the math/cmplx package. In this function, q is chosen so that real(q) >= 0 and imag(q) has
2 min read
Finding the Natural Logarithm of Complex Number in Golang
Go language provides inbuilt support for basic constants and mathematical functions for complex numbers with the help of the cmplx package. You are allowed to find the natural logarithm of the specified complex number with the help of Log() function provided by the math/cmplx package. So, you need to add a math/cmplx package in your program with th
2 min read
Finding the Hyperbolic Cosine of Complex Number in Golang
Go language provides inbuilt support for basic constants and mathematical functions for complex numbers with the help of the cmplx package. You are allowed to find the hyperbolic cosine of the specified complex number with the help of Cosh() function provided by the math/cmplx package. So, you need to add a math/cmplx package in your program with t
2 min read
Finding the Absolute Value of Specified Number in Golang
Go language provides inbuilt support for basic constants and mathematical functions to perform operations on the numbers with the help of the math package. You are allowed to find the absolute value of the specified number with the help of Abs() function provided by the math package. So, you need to add a math package in your program with the help
2 min read
Finding Arcsine of the Specified Number in Golang
Go language provides inbuilt support for basic constants and mathematical functions to perform operations on the numbers with the help of the math package. You are allowed to find the arcsine of the specified number with the help of Asin() function provided by the math package. So, you need to add a math package in your program with the help of the
2 min read
Finding Cube Root of Specified Number in Golang
Go language provides inbuilt support for basic constants and mathematical functions to perform operations on the numbers with the help of the math package. You are allowed to find the cube root of the specified number with the help of Cbrt() function provided by the math package. So, you need to add a math package in your program with the help of t
2 min read
Finding the Arccosine of Specified Number in Golang
Go language provides inbuilt support for basic constants and mathematical functions to perform operations on the numbers with the help of the math package. You are allowed to find the arccosine of the specified number with the help of Acos() function provided by the math package. So, you need to add a math package in your program with the help of t
2 min read