Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Regular Expressions in Go

Regular Expressions in Go

A regular expression (Regular Expression, abbreviated as regex or regexp) is a powerful tool for matching strings.

By defining a pattern, regular expressions can quickly search for, replace, or extract strings that match that pattern; for details, seeRegular Expression tutorial。

In Go, regular expressions are implemented through the regexp package.


In Go, the regexp package

The Go standard library provides the regexp package for handling regular expressions. The following are regexp commonly used functions and methods in the package:

  1. Compile and MustCompile


used to compile a regular expression.Compile Returns a *Regexp object and an error, while MustCompile panics directly when compilation fails.

2.

MatchString
checks whether a string matches a regular expression.

3.

FindString and FindAllString
used to find matching strings.FindString returns the first match,FindAllString returns all matches.

4.

ReplaceAllString
used to replace matching strings.

5.

Split
splits a string according to a regular expression.


Basic regular expression syntax

The following are some commonly used regular expression syntax:


Example Code

The following are some examples of using regular expressions in Go:

Example 1: Check whether a string matches a regular expression

Example

package

main

import

(

   

"fmt"

   

"regexp"

)

func

main

()

{

    pattern

:=

^[a-zA-Z0-9]+$

    regex

:=

regexp

.

MustCompile

(

pattern

)

    str

:=

"Hello123"

   

if

regex

.

MatchString

(

str

)

{

        fmt

.

Println

(

"string matches the regular expression"

)

   

}

else

{

        fmt

.

Println

(

"string does not match the regular expression"

)

   

}

}

Example 2: Find matching strings

Example

package

main

import

(

   

"fmt"

   

"regexp"

)

func

main

()

{

    pattern

:=

\d+

    regex

:=

regexp

.

MustCompile

(

pattern

)

    str

:=

"I have 3 apples and 5 bananas"

    matches

:=

regex

.

FindAllString

(

str

,

1

)

    fmt

.

Println

(

"numbers found:"

,

matches

)

}

Example 3: Replace matching strings

Example

package

main

import

(

   

"fmt"

   

"regexp"

)

func

main

()

{

    pattern

:=

\s+

    regex

:=

regexp

.

MustCompile

(

pattern

)

    str

:=

"Hello    World"

    result

:=

regex

.

ReplaceAllString

(

str

,

" "

)

    fmt

.

Println

(

"replaced string:"

,

result

)

}

Example 4: Split a string

Example

package

main

import

(

   

"fmt"

   

"regexp"

)

func

main

()

{

    pattern

:=

,

    regex

:=

regexp

.

MustCompile

(

pattern

)

    str

:=

"apple,banana,orange"

    parts

:=

regex

.

Split

(

str

,

1

)

    fmt

.

Println

(

"split string:"

,

parts

)

}


Notes

  1. Performance Issues


Regular expression matching and replacement operations can consume considerable resources, especially when processing large amounts of data. It is recommended to use them with caution in performance-sensitive scenarios.

2.

Escape character
In Go, the backslash in a regular expression \ needs to be written as \\, because the backslash is also an escape character in strings.

3.

Error handling
Use Compile function, be sure to check the returned error to avoid program crashes.