三、正则实践</h3>

go语言正则表达式查找字符串示例

<pre><code>package main import( "regexp" "fmt" ) func main() { text :="Abc a7c MFC 8ca. 你好! Golang/" //查找连续的小写字母 reg := regexp.MustCompile(`[a-z]{2,}`) fmt.Printf("%s\n", reg.FindAllString(text, -1))// [bc ca olang] } </code></pre>

查找连续的非小写字母

<pre><code> reg = regexp.MustCompile(`[^a-z]{2,}`) fmt.Printf("%s\n",reg.FindAllString(text, -1))//[ MFC 8 . 你好! G] </code></pre>

查找连续的单词字母

<pre><code> reg = regexp.MustCompile(`[a-zA-Z]{2,}`) fmt.Printf("%s\n", reg.FindAllString(text, -1))//[Abc MFC ca Golang] </code></pre>

查找连续的非单词字母、非空白字符

<pre><code> reg = regexp.MustCompile(`[^a-zA-Z\s] `) fmt.Printf("%s\n", reg.FindAllString(text, -1)) //[7 8 . 你好! /] </code></pre>

查找连续的大写字母

<pre><code> reg = regexp.MustCompile(`[A-Z] `) fmt.Printf("%s\n", reg.FindAllString(text, -1)) //[A MFC G] </code></pre>

查找 abc 或 Golang

<pre><code> reg = regexp.MustCompile(`(abc|Golang)`) fmt.Printf("%s\n", reg.FindAllString(text, -1)) //[Golang] </code></pre>

查找以 G开头,以 / 结尾的单词

<pre><code> reg = regexp.MustCompile(`G[\w.]*\/`) fmt.Printf("%s\n", reg.FindAllString(text, -1)) //[Golang/] </code></pre>

查找从行首开始,以空格结尾的字符串

<pre><code> reg = regexp.MustCompile(`[\w] \s`) fmt.Printf("%s\n", reg.FindAllString(text, -1)) //[Abc a7c MFC ] </code></pre>

查找连续 1 次到 4 次的非空格字符,并以 c 结尾的字符串

<pre><code> reg = regexp.MustCompile(`[^\s]{1,4}c`) fmt.Printf("%s\n", reg.FindAllString(text, -1)) //[Abc a7c 8c] </code></pre>

go语言正则表达式字符串替换示例

<pre><code>package main import( "regexp" "fmt" ) func main() { text :="Abc a7c MFC 8ca. 你好! Golang/" // 查找 Abc 或 a7c,替换为 Abccc a7ccc reg = regexp.MustCompile(`(Ab|a7)c`) fmt.Printf("%q\n", reg.ReplaceAllString(text, `${1}ccc`)) //"Abccc a7ccc MFC 8ca. 你好! Golang/" } </code></pre>

查找 Abc 或 a7c 或 你好 ,替换为**号

<pre><code> reg = regexp.MustCompile(`(Abc|a7c|你好)`) fmt.Printf("%s\n", reg.ReplaceAllString(text, "**")) //** ** MFC 8ca. **! Golang/ </code></pre>

在每个单词后面加上 , 号

<pre><code> reg = regexp.MustCompile(`([\w.] )\s`) fmt.Printf("%s\n", reg.ReplaceAllString(text, "${1},")) //Abc,a7c,MFC,8ca.,你好! Golang/ </code></pre>

把 a 开头的单词 更改成大写 A

<pre><code> reg = regexp.MustCompile(`[a](\w.)`) fmt.Printf("%s\n", reg.ReplaceAllString(text, `A${1}`)) //Abc A7c Aa MFC 8ca. 你好! GolAng/ </code></pre>

交换 Abc 和 Golang

<pre><code> reg = regexp.MustCompile(`(Abc)(.*)(Golang)`) fmt.Printf("%s\n", reg.ReplaceAllString(text, `${3}${2}${1}`)) //Golang a7c MFC 8ca. 你好! Abc/ </code></pre>

go语言 正则表达式 获取网页标签内的数据 示例

<pre><code>package main import( "regexp" "fmt" "io/ioutil" ) func main() { //读取文件 f,err := ioutil.ReadFile("./spider-page/regexp-1A0001.html") if err != nil { fmt.Print(err) } str := string(f) //获取 <h1>标题内的文字 reg := regexp.MustCompile(`<strong>(?s:(.*?))</strong>(.*)`) text := reg.FindAllStringSubmatch(str, -1) fmt.Printf("%s-%s\n",text[0][1],text[0][2]) // 上证指数-1A0001 } </code></pre>

获取今天最低价格

<pre><code> reg = regexp.MustCompile(`<strong id="tlowprice">(?s:(.*?))</strong>`) text = reg.FindAllStringSubmatch(str, -1) fmt.Printf("最低价:%s\n",text[0][1]) // 最低价:2533.36 </code></pre>

获取今天最高价格

<pre><code> reg = regexp.MustCompile(`<strong id="thighprice">(?s:(.*?))</strong>`) text = reg.FindAllStringSubmatch(str, -1) fmt.Printf("最高价:%s\n",text[0][1]) // 最高价:2554.79 </code></pre>

获取当前时间

<pre><code> reg = regexp.MustCompile(`<span class="price" id="hexm_curPrice">(?s:(.*?))</span>`) text = reg.FindAllStringSubmatch(str, -1) fmt.Printf("当前价:%s\n",text[0][1]) // 当前价:2553.83 </code></pre>

获取当前价格

<pre><code> reg = regexp.MustCompile(`<p id="timeshow">(?s:(.*?))</p>`) text = reg.FindAllStringSubmatch(str, -1) fmt.Printf("当前时间:%s\n",text[0][1]) // 当前时间:2019年1月12日 15:00:00 </code></pre>

regexp-1A0001.html(示例文件)

<pre><code><h1 class="m_logo fl"> <strong>上证指数</strong>1A0001 </h1> <div class="new_detail fl"> <div class="price_box fl price_box_wide" id="hexm_curPrice_wrap"> <div class="clearfix plus_price" id="quote_header"> <span class="price" id="hexm_curPrice">2553.83</span> <div class="price_details"> <span class="plus_rate" id="hexm_float_price">18.73</span> <span class="price_plus" id="hexm_float_rate">0.74%</span> </div> </div> <div class="minute_price ting clearfix" id="quote_header2"> <span class="price"><a href="###">停牌</a></span> </div> <div class="minute_price_ztdt fl"> <p>涨停:<span class="red" id="tmaxprice">0</span>跌停:<span class="green" id="tminprice">0</span></p> <p id="timeshow">2019年1月12日 15:00:00</p> </div> </div> <ul class="new_trading fl"> <li> <span class="jk">今开:<strong class="minus" id="topenprice">2539.55</strong></span> <span class="contract_amount">成交量:<strong id="tamount" class="plus">149.4亿</strong></span> <span class="zf">振幅:<strong id="trange">0.85%</strong></span> </li> <li> <span class="zg">最高:<strong id="thighprice">2554.79</strong></span> <span class="trading_volume">成交额:<strong id="tamounttotal">1223.8亿</strong></span> <span class="hs">换手:<strong id="tchange">0.45%</strong></span> </li> <li> <span class="zd">最低:<strong id="tlowprice">2533.36</strong></span> <span class="trading_volume">总市值:<strong id="tvalue">276895.2</strong>亿</span> <span class="sjl">市净率:<strong id="tvaluep">0</strong></span> </li> <li> <span class="zs">昨收:<strong id="pprice">2535.10</strong></span> <span class="trading_volume">流通市值:<strong id="flowvalue">238534.6</strong>亿</span> <span class="syl">市盈率(动):<strong id="fvaluep">亏损</strong></span> </li> </ul> </div> </code></pre> 到此这篇关于“【golang】正则表达式 查找和替换字符”的文章就介绍到这了,更多文章或继续浏览下面的相关文章,希望大家以后多多支持JQ教程网!