Golang Regular module use

Source: Internet
Author: User
Tags k8s
This is a creation in Article, where the information may have evolved or changed.

Recently in the development process will encounter some string matching related content, just to learn about the next Golang in the regexp module. Because the regular module for me is more about matching and processing strings, there are several ways to return as string types.

regexpStructure and method definition of a module

//正则结构体type Regexp struct {        // contains filtered or unexported fields}//初始化结构体对象的方法func Compile(expr string) (*Regexp, error)func CompilePOSIX(expr string) (*Regexp, error)func MustCompile(str string) *Regexpfunc MustCompilePOSIX(str string) *Regexp//结构体方法.常用的几个//在字符串s中查找完全匹配正则表达式re的字符串.如果匹配到就停止不进行全部匹配,如果匹配不到就输出空字符串func (re *Regexp) FindString(s string) string//在字符串s中匹配re表达式,n表示匹配的次数,-1表示匹配整个字符串。返回字符串切片func (re *Regexp) FindAllString(s string, n int) []string//在src中匹配re,并替换为repl,该种方式中repl中的$符号会展开实际的变量,通常用在回溯查找中func (re *Regexp) ReplaceAllString(src, repl string) string//在src中匹配re,并替换为repl,该方法会按照repl中的字面意思进行替换,不支持高级变量匹配,比如回溯等等func (re *Regexp) ReplaceAllLiteralString(src, repl string) string//在字符串中是否匹配到re定义的字符串,匹配返回truefunc (re *Regexp) MatchString(s string) bool

Simple example

$ cat regexp-test.go/** * @File Name:test.go * @Author: * @Email: * @Create date:2017-12-24 15:12:31 * @Last modified:2 017-12-24 16:12:12 * @Description: */package mainimport ("FMT" "RegExp") func main () {teststring: = "k8s-test-pod-12343k81 1sadsxsakxz-test-k8s-container.k8s-@xxbandy. Github.io "Re: = RegExp.  Mustcompile ("K8s?") Fmt. Println ("src string:", teststring) fmt. Println ("Regular expression:", re) fmt. Println ("Findallstring matching All:", re. Findallstring (teststring,-1)) fmt. Println ("Findallstring matching twice:", re. Findallstring (teststring,2)) fmt. Println ("FindString:", re.) FindString (teststring)) fmt. Println ("replaceallstring:", re.) Replaceallstring (teststring, "Biaoge")) fmt. Println ("replaceallliteralstring:", re.) Replaceallliteralstring (teststring, "Biaoge")) fmt. Println ("Match String:", re.) Matchstring (teststring))}$ go run regexp-test.gosrc string: k8s-test-pod-12343k811sadsxsakxz-test-k8s-container.k8s-@xxbandy. Github.ioregular expression:k8s? findallstring matching all: [k8s K8 k8s k8s]findallstring matching twice: [k8s k8]findstring:k8sreplaceallstring: biaoge-test-pod-12343biaoge11sadsxsakxz-test-biaoge-container.biaoge-@xxbandy. github.ioreplaceallliteralstring : biaoge-test-pod-12343biaoge11sadsxsakxz-test-biaoge-container.biaoge-@xxbandy. Github.iomatch string:true

Regardless of the language of the regular module, the individual believes that grammar is not the most important, the most important I think is the regular expression itself, if the regular expression itself know more deeply, regardless of the language tool can be very flexible processing of various business scenarios. Here is an article related to regular expressions written in the year

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.