Go offers built-in support for regular expressions. Here are some examples of common regexp-related tasks in Go. |
|
|
|
|
|
|
|
This tests whether a pattern matches a string. |
|
Above we used a string pattern directly, but for
other regexp tasks you’ll need to |
|
Many methods are available on these structs. Here’s a match test like we saw earlier. |
|
This finds the match for the regexp. |
|
This also finds the first match but returns the start and end indexes for the match instead of the matching text. |
|
The |
|
Similarly this will return information about the indexes of matches and submatches. |
|
The |
|
These |
|
Providing a non-negative integer as the second argument to these functions will limit the number of matches. |
|
Our examples above had string arguments and used
names like |
|
When creating global variables with regular
expressions you can use the |
|
The |
|
The |
|
|
|
For a complete reference on Go regular expressions check
the |
Next example: JSON.