Regular expression to extract all Non-Alphanumeric Characters from a String
Example
package main
import (
"fmt"
"regexp"
)
func main() {
str1 := "We @@@Love@@@@ #Go!$! ****Programming****Language^^^"
re := regexp.MustCompile(`[^a-zA-Z0-9]+`)
fmt.Printf("Pattern: %v\n", re.String()) // print pattern
fmt.Println(re.MatchString(str1)) // true
submatchall := re.FindAllString(str1, -1)
for _, element := range submatchall {
fmt.Println(element)
}
}
Output
Pattern: [^a-zA-Z0-9]+
true
@@@
@@@@ #
!$! ****
****
^^^
Most Helpful This Week
Example: Split, Join, and Equal from BYTES Package
What is Rune? How to get ASCII value of any character in Go?
How to play and pause execution of goroutine?
How to check if a string contains a substring in Golang?
Subtract N number of Year, Month, Day, Hour, Minute, Second, Millisecond, Microsecond and Nanosecond to current date-time.
How to Decode or Unmarshal bi-dimensional array of integers?
Replace numbers by zero from string
Golang Convert String into Snake Case
Replace any non-alphanumeric character sequences with a dash using Regex
How to create thumbnail of an image?