Neurohazard
暮雲煙月,皓首窮經;森羅萬象,如是我聞。

Golang 中的正则表达式

wpadmin~September 2, 2019 /System Management

Golang 中的正则表达式

正文

https://stackoverflow.com/questions/30483652/how-to-get-capturing-group-functionality-in-go-regular-expressions/39635221#39635221

package main

import (
    "fmt"
    "regexp"
)

func main() {
    dumpString := `<ul id="poweredby">
            <li class="noprint">Powered by <a href="http://www.atlassian.com/software/confluence" class="hover-footer-link" rel="nofollow">Atlassian Confluence</a> <span id='footer-build-information'>6.10.2</span></li>
            <li class="print-only">Printed by Atlassian Confluence 6.10.2</li>
            <li class="noprint"><a href="https://support.atlassian.com/help/confluence" class="hover-footer-link" rel="nofollow">Report a bug</a></li>
            <li class="noprint"><a href="http://www.atlassian.com/about/connected.jsp?s_kwcid=Confluence-stayintouch" class="hover-footer-link" rel="nofollow">Atlassian News</a></li>
        </ul>`
    re := regexp.MustCompile(`(?m)<span id='footer-build-information'>(?P<Version>.*)</span>`)
    match := re.FindStringSubmatch(dumpString)
    versionString := match[0]
    fmt.Println(versionString)
}

Leave a Reply

Your email address will not be published. Required fields are marked *