Current location - Training Enrollment Network - Mathematics courses - Golang regular regular expression package use
Golang regular regular expression package use
Firstly, several commonly used methods are introduced:

1. Use the MatchString function or the Match function.

Regular expression. Matchstring (patternstring, ss string) pattern is a regular expression, and s is the string to be checked.

Regular expression. Match (patternstring, b [] byte) pattern is a regular expression, and s is the string to be checked.

Their function is matching, but the difference is that the parameters are strings and slices.

Examples are as follows:

2. Use the Compile function or the MustCompile function.

The difference between them is that Compile returns two parameters of type Regexp, error, while MustCompile only returns type Regexp.

Their function is to compile regular expressions and return the optimized Regexp structure, which requires many methods.

Examples are as follows:

3. Find a regular matching string (note: the parameter passed in the function name containing string is string, and everything else is []byte with all).

Find the position of a string that matches the rule (note: the parameter passed by the function name containing string is string, and the others are []byte with all).

Step 4 replace

Conventional replacement

Replace with original text

Function replaces the source string

5. Some common methods in regular expression structure.