Golang使用正则
正则在线测试网站
Golang标准库——regexp
相关文章
浅析golang 正则表达式
https://www.admin5.com/article/20201020/971080.shtml
Golang 正则表达式(regexp)
https://blog.csdn.net/guyan0319/article/details/87873070
课程学习地址:
https://www.bilibili.com/video/BV1Nt411H7sP/?p=12
手册地址:
https://studygolang.com/pkgdoc
主要使用 regexp 包
http://www.topgoer.com/并发编程/爬虫小案例.html?h=正则表达




x*? 重复>=0次匹配x,越少越好(优先跳出重复)

dome
package main
import (
"fmt"
"regexp"
)
func main() {
demo03()
}
func demo03(){
str := `
<!DOCTYPE html>
<!--STATUS OK-->
<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<link rel="search" type="application/opensearchdescription+xml" href="/tb/cms/content-search.xml" title="百度贴吧" />
<meta itemprop="dateUpdate" content="2020-04-08 17:18:45" />
<meta name="keywords" content="绝地求生,客户端网游,游戏,哥哥,柏林">
<meta name="description" content="本吧热帖: 1-144hz可以稳130fps吗 2-有一起的不 3-好酸啊 哥哥柏林可真牛呢 别人人均酸? 这一句真以为我会跟贴 4-有没有pubg每天能打打游戏的群~ 5-给?爬进来挨打,拉视频上身份证 6-【2020吧规重置版】绝地求生吧改革制度实行办法(最新修订) 7-绝地求生冠军联赛(PCL) 2020春季赛开赛公告 8-PUBG:关于正式服现存问题的应对措施目前正式服存在多种问题">
<title>绝地求生吧-百度贴吧 </title>
<link rel="stylesheet" href="//tb1.bdstatic.com/??/tb/static-common/style/tb.js/dialog_bda1025.css,/tb/static-common/lib/tbui/style/all_f29e774.css" />
<link rel="stylesheet" href="//tb1.bdstatic.com/??/tb/_/index_4fc89ea.css,/tb/_/search_8bbfc49.css,/tb/_/http_transform_d41d8cd.css,/tb/_/frs-footer/content_footer_e1ac3c2.css,/tb/_/fixed_bar_af8c791.css,/tb/_/fixed_daoliu_c2042f0.css,/tb/_/frs-footer/extension_d41d8cd.css,/tb/_/suggestion_c2d979b.css,/tb/_/page_router_6d81cff.css,/tb/_/login_dialog_4a340b6.css,/tb/_/icons_fba8214.css,/tb/_/base_user_data_f665ab7.css,/tb/_/base_dialog_user_bar_69fdf18.css,/tb/_/qianbao_cashier_dialog_179e56e.css,/tb/_/qianbao_purchase_member_f0586a7.css,/tb/_/cashier_dialog_0d74ed0.css,/tb/_/pay_member_d41d8cd.css,/tb/_/new_message_system_bea7f16.css,/tb/_/user_head_35f26e0.css,/tb/_/js_pager_5be1e39.css,/tb/_/wallet_dialog_fce7ffd.css,/tb/_/card_1f33a7b.css,/tb/_/userbar_b56525c.css,/tb/_/duoku_servers_dialog_f50364d.css,/tb/_/duoku_servers_list_42e14c2.css,/tb/_/poster/rich_poster_4d57f00.css,/tb/_/footer_fd940ae.css,/tb/_/head_main_6892579.css,/tb/_/full_width_head_ca1a4d5.css,/tb/_/forum_card_62fcc00.css" />
<link rel="stylesheet" href="//tb1.bdstatic.com/??/tb/_/qianbao_purchase_tdou_4b31f54.css,/tb/_/payment_dialog_title_5433211.css,/tb/_/tdou_get_1536ffc.css,/tb/_/paykey_safe_payment_8f2a8db.css,/tb/_/captcha_30e4d3c.css,/tb/_/captcha_safe_payment_d41d8cd.css,/tb/_/mobile_safe_payment_77949bb.css,/tb/_/tbean_safe_3c779a3.css,/tb/_/tbean_safe_ajax_d41d8cd.css,/tb/_/umoney_f12b09a.css,/tb/_/tdou_template_3eae00b.css,/tb/_/umoney_query_d15a716.css,/tb/_/tdou_d41d8cd.css,/tb/_/send_gift_success_24ee666.css,/tb/_/gift_page_ctrl_eac352f.css,/tb/_/gift_loading_css_e2c23e9.css,/tb/_/send_gift_dialog_0b498fd.css,/tb/_/raking_gift_dialog_da61760.css,/tb/_/gift_batou_goin_d46b5b1.css,/tb/_/bubble_tip_17a9ec9.css,/tb/_/tbshare_share_4a2fcf9.css,/tb/_/tbshare_popup_d41d8cd.css,/tb/_/aside_float_bar_277a843.css,/tb/_/verify_manager_phone_7d1435e.css,/tb/_/bawu_add_section_cb95883.css,/tb/_/icon_tip_db299f2.css,/tb/_/firework_v2_2e35f90.css,/tb/_/novel_icons_02ab048.css,/tb/_/global_notice_8c177cf.css,/tb/_/umoney_promotion_dialog_d0d023b.css" />
<link rel="stylesheet" href="//tb1.bdstatic.com/??/tb/_/snowflow_45a89bb.css,/tb/_/dialog_6ed86bb.css,/tb/_/cont_sign_card_73a332e.css,/tb/_/buy_controller_a328148.css,/tb/_/tieba_sign_card_2928c29.css,/tb/_/placeholder_7eb7ce6.css,/tb/_/sms_verify_dialog_cd843b0.css,/tb/_/forbidden_752e552.css,/tb/_/poster/share_thread_cbacfa9.css,/tb/_/like_tip_65eb23b.css,/tb/_/poster/topic_suggestion_c9a8071.css,/tb/_/word_limit_3c5481d.css,/tb/_/complaint_bar_owner_3dd0d03.css,/tb/_/poster/rich_poster_e538a98.css,/tb/_/ueditor_base_60e7ce4.css,/tb/_/ueditor_extend_base_d41d8cd.css,/tb/_/background_c5ba91f.css,/tb/_/tb_gram_d41d8cd.css,/tb/_/slide_select_51d60fb.css,/tb/_/image_flash_editor_8f43e09.css,/tb/_/scroll_panel_eb74727.css,/tb/_/picture_uploader_7e6cefb.css,/tb/_/picture_web_selector_e53d66f.css,/tb/_/picture_59387e8.css,/tb/_/custom_emotion_2d0490a.css,/tb/_/ueditor_emotion_a5eeac8.css,/tb/_/ueditor_video_fbebf66.css,/tb/_/sketchpad_cf60180.css,/tb/_/scrawl_5840a35.css,/tb/_/ueditor_topic_bb19767.css" />
<link rel="stylesheet" href="//tb1.bdstatic.com/??/tb/_/topic_suggestion_3234253.css,/tb/_/fullscreen_f64a424.css,/tb/_/at_d03b8c9.css,/tb/_/post_setting_46ea748.css,/tb/_/setting_ca19f87.css,/tb/_/medal_5022a4b.css,/tb/_/paypost_agree_dialog_fd57709.css,/tb/_/paypost_editor_6d704da.css,/tb/_/attention_category_game_d2d4220.css,/tb/_/focus_btn_21ad291.css,/tb/_/tbnav_bright_a02e0ea.css,/tb/_/conf_repost_2d678d2.css,/tb/_/iframe_head_b5db402.css,/tb/_/activity_btv_5b32c41.css,/tb/_/game_iframe_common_19e3259.css,/tb/_/game_banner_ff14_9ad227a.css,/tb/_/game_head_info_765f80b.css,/tb/_/duoku_xwj_vip_e9fab16.css,/tb/_/wca_head_49a2ff8.css,/tb/_/athletic_center_loader_d41d8cd.css,/tb/_/game_player_auth_dialog_2e5deb0.css,/tb/_/sign_mod_539e18c.css,/tb/_/sign_tip_98d0754.css,/tb/_/game_frs_in_head_8026069.css,/tb/_/game_rank_in_head_94ba4ce.css,/tb/_/game_frs_head_218209e.css,/tb/_/platform_spread_layer_b5ef871.css,/tb/_/platform_spread_video_070115e.css,/tb/_/top_activity_e12a4fe.css,/tb/_/popup_zhang_8b74e36.css" />
<link rel="stylesheet" href="//tb1.bdstatic.com/??/tb/_/block_user_1e8ac98.css,/tb/_/music_player_0a327b2.css,/tb/_/game_code_thread_c9a2228.css,/tb/_/game_thread_d41d8cd.css,/tb/_/single_icons_3112de2.css,/tb/_/post_marry_1ed5b11.css,/tb/_/interaction_d4668aa.css,/tb/_/month_icon_240ecc1.css,/tb/_/user_visit_card_129ebe7.css,/tb/_/util_pop_video_5116377.css,/tb/_/voice_c5bf26e.css,/tb/_/thread_list_7faa4cd.css,/tb/_/aside_region_6df4cfc.css,/tb/_/frs-aside/app_download_d024b8b.css,/tb/_/lecai_iframe_a48aee4.css,/tb/_/professional_manager_tips_af0267b.css,/tb/_/by_forum_db9c68b.css,/tb/_/celebrity_widget_974def6.css,/tb/_/celebrity_forum_dialog_b44a28b.css,/tb/_/forum_member_dialog_1d49009.css,/tb/_/exp_package_dialog_5cb5fdb.css,/tb/_/npc_vote_action_5b250b1.css,/tb/_/celebrity_81c8269.css,/tb/_/celebrity_expball_e0bb045.css,/tb/_/frs-aside/forum_info_415639b.css,/tb/_/mixin_bd9244b.css,/tb/_/bean_d41d8cd.css,/tb/_/guess_3c31a66.css,/tb/_/sidebar_2541a8b.css,/tb/_/frs-aside/search_back_2dd1855.css" />
<link rel="stylesheet" href="//tb1.bdstatic.com/??/tb/_/thread_item_44be836.css,/tb/_/thread_item_title_88b0c21.css,/tb/_/frs_user_base_e6ee6b4.css,/tb/_/util_pager_fd327a7.css,/tb/_/thread_list_footer_99af8d2.css" />
<link rel="shortcut icon" href="https://gsp0.baidu.com/5aAHeD3nKhI2p27j8IqW0jdnxx1xbK/tb/favicon.ico"/>
</head>
<body>
<div id="head" class="search_bright clearfix">
<div class="head_inner">
<div class="search_top clearfix">
<div class="search_nav j_search_nav">
<!-- 资讯、贴吧、知道、视频、音乐、图片、地图、文库 -->
<a rel="noreferrer" param="wd"
href=http://www.baidu.com/s?cl=3& >网页</a>
<a rel="noreferrer" param="word" href="http://www.baidu.com/s?rtt=1&bsst=1&cl=2&tn=news&fr=tieba&">资讯</a>
<b>贴吧</b>
<a rel="noreferrer" param="word"
href="http://zhidao.baidu.com/q?ct=17&pn=0&tn=ikaslist&rn=10&">知道</a>
<!-- <a rel="noreferrer" param="word" href="http://v.baidu.com/v?ct=301989888&rn=20&pn=0&db=0&s=21&">视频</a> -->
<a rel="noreferrer" param="word" href="http://www.baidu.com/sf/vsearch?pd=video&tn=vsearch&ct=301989888&rn=20&pn=0&db=0&s=21&rsv_spt=11&">视频</a>
<a rel="noreferrer" param="key" href="http://music.baidu.com/search?fr=tieba&">音乐</a>
<a rel="noreferrer" param="word"
href="http://image.baidu.com/i?tn=baiduimage&ct=201326592&lm=-1&cl=2&">图片</a>
<a rel="noreferrer" param="word" href="http://map.baidu.com/m?fr=map006&">地图</a>
<a rel="noreferrer" href="http://wenku.baidu.com/search?fr=tieba&lm=0&od=0&" param="word" target="_blank">文库</a>
</div>
</div>
<div>hello regexp</div>
<div>hello 2</div>
<div>hello 890</div>
<div>hello 123</div>
<div>
长歌行
青青园中葵,朝露待日晞。
阳春布德泽,万物生光辉。
常恐秋节至,焜黄华叶衰。
百川东到海,何时复西归?
少壮不努力,老大徒伤悲。
</div>
</div>
</body>
</html>`
//regestr := `<div>.*</div>` //匹配不到换行的 [[<div>hello regexp</div>] [<div>hello 2</div>] [<div>hello 890</div>] [<div>hello 123</div>]]
//regestr := `param="word" href="(?s:(.*?)).*">` // ?s: 单选匹配 `x*?` 重复>=0次匹配x,越少越好(优先跳出重复)
regestr := `<div>(?s:(.*?))</div>`
reg := regexp.MustCompile(regestr) //解析,编译正则表达式 ``表示使用原生字符串
resdata:=reg.FindAllStringSubmatch(str,-1) //提取需要的信息 -1代码匹配全部, n代码匹配n次
//fmt.Println(resdata)
for _,v:=range resdata{
fmt.Println("取不带标识的内容v[1]:",v[1])
}
}
func demo02(){
str := "3.14 123.123 .68 haha 1.0 abc 7. ab.3 66.6 123."
//regestr := `[0-9]+\.[0-9]+` //[[3.14] [123.123] [1.0] [66.6]]
//regestr := `\d+\.\d+` //[[3.14] [123.123] [1.0] [66.6]]
//regestr := `\d?\.\d+` //[[3.14] [3.123] [.68] [1.0] [.3] [6.6]]
regestr := `\d\.\d` //[[3.1] [3.1] [1.0] [6.6]]
reg := regexp.MustCompile(regestr) //解析,编译正则表达式 ``表示使用原生字符串
resdata:=reg.FindAllStringSubmatch(str,-1) //提取需要的信息 -1代码匹配全部, n代码匹配n次
fmt.Println(resdata)
}
func demo01(){
str := "abc a7c mfc cat 8ca azc cba aBc"
//regestr := `a.c` //[[abc] [a7c] [azc] [aBc]]
//regestr := `a[0-9]c` //[[a7c]]
regestr := `a[0-9a-z]c` //[[abc] [a7c] [azc]]
//regestr := `a[^0-9a-z]c` //[[aBc]]
reg := regexp.MustCompile(regestr) //解析,编译正则表达式 ``表示使用原生字符串
resdata:=reg.FindAllStringSubmatch(str,-1) //提取需要的信息 -1代码匹配全部, n代码匹配n次
//resdata:=reg.FindAllString(str,-1) //提取需要的信息 -1代码匹配全部, n代码匹配n次
fmt.Println(resdata)
}
Golang使用正则的更多相关文章
- 基于 esbuild 的 universal bundler 设计
--字节跳动前端 Byte FE :杨健 背景 由于 Lynx(公司自研跨端框架)编译工具和传统 Web 编译工具链有较大的差别(如不支持动态 style 和动态 script 基本告别了 bundl ...
- golang 常用的正则查找与替换
package main; import ( "regexp" "fmt" "strings" ) func main() { //1.过正 ...
- 学习正则 - golang实现
元字符: 表1.常用的元字符 代码 说明 . 匹配除换行符以外的任意字符 \w 匹配字母或数字或下划线或汉字 \s 匹配任意的空白符 \d 匹配数字 \b 匹配单词的开始或结束 ^ 匹配字符串的开始 ...
- Golang 正则匹配 -- regexp
匹配特殊字符 //re :=regexp.MustCompile("[~!@#$%^&*(){}|<>\\\\/+\\-[]:\"?'::''"&qu ...
- 基础知识 - Golang 中的正则表达式
------------------------------------------------------------ Golang中的正则表达式 ------------------------- ...
- golang 如何验证struct字段的数据格式
本文同时发表在https://github.com/zhangyachen/zhangyachen.github.io/issues/125 假设我们有如下结构体: type User struct ...
- golang web实战之二(iris)
之前写了一篇为:golang web实战之一(beego,mvc postgresql) 听说iris更好: 1. iris hello world package main import &quo ...
- adb常用命令(golang版)及输入中文
package main import ( "crypto/md5" "fmt" "image/png" "io/ioutil&q ...
- Golang 入门 : 字符串
在 Golang 中,字符串是一种基本类型,这一点和 C 语言不同.C 语言没有原生的字符串类型,而是使用字符数组来表示字符串,并以字符指针来传递字符串.Golang 中的字符串是一个不可改变的 UT ...
- golang 使用pprof和go-torch做性能分析
软件开发过程中,项目上线并不是终点.上线后,还要对程序的取样分析运行情况,并重构现有的功能,让程序执行更高效更稳写. golang的工具包内自带pprof功能,使找出程序中占内存和CPU较多的部分功能 ...
随机推荐
- spark和hadoop的区别
hadoopHadoop是一个由Apache基金会所开发的分布式系统基础架构. 用户可以在不了解分布式底层细节的情况下,开发分布式程序.充分利用集群的威力进行高速运算和存储. Hadoop实现了一个分 ...
- 4 CSS属性选择器
4 属性选择器 属性选择器是通过元素的属性及属性值来选择元素的.下面介绍属性选择器的用法. 第一种用法 作用:选择含有指定属性的元素. 语法:[属性名]{} 示例如下: <!DOCTYPE ht ...
- 使用OHOS SDK构建benchmark
参照OHOS IDE和SDK的安装方法配置好开发环境. 从github下载源码. 执行如下命令: git clone --depth=1 https://github.com/google/bench ...
- Qt操作sqlite数据库
代码讲解: 1.检查数据库文件是否存在,如果不存在就创建数据库文件 2.创建 person 表(等下的操作就是操作这个表) 3.查询出 person 表中所有的数据,并显示出来 Pro 文件 添加 S ...
- 【Java面试指北】单例模式
单线程下的单例模式: public class Singleton { private static Singleton instance; private Singleton() {} public ...
- 打造美团外卖新体验,HarmonyOS SDK 持续赋能开发者共赢鸿蒙生态
从今年 8 月起,所有升级到 HarmonyOS 4 的手机用户在美团外卖下单后,可通过屏幕上的一个"小窗口",随时追踪到"出餐.取餐.送达"等订单状态.这个能 ...
- CentOS 6.5快速部署HTTP WEB服务器和FTP服务器
CentOS 6.5快速部署HTTP WEB服务器和FTP服务器 时间:2014-03-29 来源:服务器之家 投稿:root 点击:210次 [题记]本文使用CentOS 6.5m ...
- 重新点亮linux 命令树————二进制安装[十一八]
前言 简单介绍一下二进制安装 正文 wget https://openresty.org/download/openresty-1.15.8.1.tar.gz tar -zxf openresty-V ...
- Linux命令之查找CPU资源利用情况(lscpu和top详解)
1.lscpu命令:获取CPU架构完整详细信息,例如架构信息,CPU模式,CPU频率,CPU核心数.线程数.缓存大小. 在终端输入"lscpu": 参数详解: [Architect ...
- 基于信通院 Serverless 工具链模型的实践:Serverless Devs
简介: Serverless Devs 作为开源开放的开发者工具,参编中国信通院<基于无服务器架构的工具链能力要求>标准,为行业统一规范发挥助推作用! 作者 | 江昱(阿里云 Serve ...