Golang | Creating a string that contains regexp metacharacters
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 create a string that escapes all regular expression metacharacters in the specified text with the help of QuoteMeta() function. The returned string of this function is a regular expression matching the literal text. This function is defined under the regexp package, so for accessing this method you need to import the regexp package in your program.
Syntax:
func QuoteMeta(str string) string
Example 1:
// Go program to illustrate how to create // a string that escapes all regular // expression metacharacters package main import ( "fmt" "regexp" ) // Main function func main() { // Escaping all regular // expression metacharacters // Using QuoteMeta () function res1 := regexp.QuoteMeta(`String 1: .+*()|[]{}^$`) fmt.Println(res1) res2 := regexp.QuoteMeta(`String 2: +()*`) fmt.Println(res2) res3 := regexp.QuoteMeta(`String 3: []|{}$`) fmt.Println(res3) res4 := regexp.QuoteMeta(`String 4: ^$*-,`) fmt.Println(res4) } |
Output:
String 1: \.\+\*\(\)\|\[\]\{\}\^\$ String 2: \+\(\)\* String 3: \[\]\|\{\}\$ String 4: \^\$\*-,
Example 2:
// Go program to illustrate how to create // a string that escapes all regular // expression metacharacters package main import ( "fmt" "regexp" ) // Main function func main() { // Creating and initializing string // Using shorthand declaration s1 := `+*?()|[]^$` s2 := `+*?()|[]^$ ` if s1 == s2 { // Escaping all regular // expression metacharacters // Using QuoteMeta () function res := regexp.QuoteMeta(s1) fmt.Println( "String:" , res) } else { fmt.Println( "Not Equal" ) } } |
Output:
Not Equal
Recommended Posts:
- How to Trim a String in Golang?
- How to Split a String in Golang?
- Golang | Splitting the string after the specified separator
- How to find the last index value of specified string in Golang?
- How to check the specified rune in Golang String?
- How to convert a string in uppercase in Golang?
- How to Replace Characters in Golang String?
- How to find the index value of specified string in Golang?
- Golang | How to find the index of rune in the string?
- Check if the given characters is present in Golang String
- Golang | Extracting a Regular Expression from the String
- Golang | Checking the string for the specified regular expression
- Golang | Extracting all the Regular Expression from the String
- Check if the String starts with specified prefix in Golang
- How to convert a string in lower case in Golang?
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.