分享
golang regexp Flag 用法匹配换行
崔天浩 · · 3549 次点击 · · 开始浏览这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。
| Flag | 含义 |
|---|---|
| i | 不区分大小写 |
| s | 让.匹配\n(单行模式) |
| m | 除了开始/结束文本外,让^并$匹配开始/结束行(多行模式) |
用法
例如:前缀"(?is)"使匹配的字符不区分大小写,并让. 匹配 \n
package main
import (
"fmt"
"regexp"
)
const CONTENT = `<div class="info">
<div class="hd">
<a href="https://movie.douban.com/subject/1291546/" class="">
<span class="title">霸王别姬</span>
<span class="other"> / 再见,我的妾 / Farewell My Concubine</span>
</a>
`
func main() {
a := regexp.MustCompile(`(?is:class.*?</span>)`)
fmt.Println(a)
fmt.Println(a.FindAllString(CONTENT, -1))
}
输出:
(?is:class.*?</span>)
[class="info"> <div class="hd"> <a href="https://movie.douban.com/subject/1291546/" class=""> <span class="title">霸王别姬</span> class="other"> / 再见,我的妾 / Farewell My Concubine</span>]
有疑问加站长微信联系(非本文作者)
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
关注微信3549 次点击
添加一条新回复
(您需要 后才能回复 没有账号 ?)
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码` - 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传
收入到我管理的专栏 新建专栏
| Flag | 含义 |
|---|---|
| i | 不区分大小写 |
| s | 让.匹配\n(单行模式) |
| m | 除了开始/结束文本外,让^并$匹配开始/结束行(多行模式) |
用法
例如:前缀"(?is)"使匹配的字符不区分大小写,并让. 匹配 \n
package main
import (
"fmt"
"regexp"
)
const CONTENT = `<div class="info">
<div class="hd">
<a href="https://movie.douban.com/subject/1291546/" class="">
<span class="title">霸王别姬</span>
<span class="other"> / 再见,我的妾 / Farewell My Concubine</span>
</a>
`
func main() {
a := regexp.MustCompile(`(?is:class.*?</span>)`)
fmt.Println(a)
fmt.Println(a.FindAllString(CONTENT, -1))
}
输出:
(?is:class.*?</span>)
[class="info"> <div class="hd"> <a href="https://movie.douban.com/subject/1291546/" class=""> <span class="title">霸王别姬</span> class="other"> / 再见,我的妾 / Farewell My Concubine</span>]