Home » Go Regex

Go Regex

Go Regex package is used for searching string. To search a string, we need to provide a pattern of the string.

We need to compile the pattern into the regex object so that we can invoke methods through it.

The regex object can be retrieved by using compile() and mustcompile() functions. Now we can use functions to find strings such as FindString(), FindStringSubmatch(), FindStringIndex() etc.

Go Regex Example 1

Output:

.com  .com 

The FindString() method returns a string having the text of the left most match. If no match is found, empty string is returned.

Go Regex Example 2

Output:

[6 10] [] [2 6] 

Go Regex Example 3

we can also use FindStringSubmatch() method which returns a slice of strings having the text of the leftmost match and the matches. If no match is found, the return value is an empty string.

Output:

[flying ly] [floating loat]  Process finished with exit code 0 

Next TopicGo Struct

You may also like