Friday, April 26, 2024
HomeGolangTake away non-alphanumeric characters from a string in Go (Golang)

Take away non-alphanumeric characters from a string in Go (Golang)



To clear a string from all non-alphanumeric characters in Go, it’s best to make use of a common expression that matches non-alphanumeric characters, and substitute them with an empty string. Such a regex is outlined because the negation of the set of allowed characters. For the English alphabet, it will be:

[^a-zA-Z0-9 ]+

On this common expression, we use the Caret character after a sq. bracket [^, which means that it matches any character other than a lowercase and uppercase letter in the range a-z, a number, and a space character. “Not a letter, not a number, not a space” is our definition of a non-alphanumeric character.

However, when working with alphabets other than English, such a regular expression will not work properly. In that case, we can use the Unicode categories in the regex and instead of manually defining the range of letters and numbers, we can use the Unicode category p{L} for letters and p{N} for numbers:

[^p{L}p{N} ]+

This common expression matches any character that isn’t a Unicode letter and quantity or an area character.

To match these common expressions and the outcomes of features that take away non-alphanumeric characters, see the next two examples.

Take away all non-alphanumeric characters for English alphabet strings

This can be a basic instance of eradicating non-alphanumeric characters from a string. First, we compile our common expression that matches any character aside from an English letter, quantity, or house. Then, we use the Regexp.ReplaceAllString() methodology to switch the matched non-alphanumeric characters with the empty string "". Have a look at the output and spot that this methodology removes each non-English letters (ـا, ą) and numbers (٦).

bundle essential

import (
    "fmt"
    "regexp"
)

var nonAlphanumericRegex = regexp.MustCompile(`[^a-zA-Z0-9 ]+`)

func clearString(str string) string {
    return nonAlphanumericRegex.ReplaceAllString(str, "")
}

func essential() {
    str := "Take a look at@%String#321gosamples.dev ـا ą ٦"
    fmt.Println(clearString(str))
}

Output:

TestString321gosamplesdev

Take away all non-alphanumeric characters for non-English alphabet strings

This instance works just like the earlier one, however through the use of an everyday expression with Unicode classes, we additionally settle for letters and numbers from alphabets aside from English, comparable to Arabic.

bundle essential

import (
    "fmt"
    "regexp"
)

var nonAlphanumericRegex = regexp.MustCompile(`[^p{L}p{N} ]+`)

func clearString(str string) string {
    return nonAlphanumericRegex.ReplaceAllString(str, "")
}

func essential() {
    str := "Take a look at@%String#321gosamples.dev ـا ą ٦"
    fmt.Println(clearString(str))
}

Output:

TestString321gosamplesdev ـا ą ٦
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments