课程学习地址:
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次
fmt.Println(resdata)
}