正则替换 http 为 https,中间部分如何匹配但不存储? - V2EX
请不要在回答技术问题时复制粘贴 AI 生成的内容
Flands

正则替换 http 为 https,中间部分如何匹配但不存储?

  •  
  •   Flands Apr 8, 2019 2870 views
    This topic created in 2617 days ago, the information mentioned may be changed or developed.

    有字符串
    fasuehttp://www.google.comfasdfhttp://www.baidu.comfasdhttp://sougou.comsfdhttp://www.zhihu.com
    要求:
    只将结尾为google | sougouhttp改为https

    我给的正则无效
    http:(?:(.{0,8}))(?=(?:(google|sougou)))
    如果只存在http://www.xxxxx的话很好匹配,但是不确定www是否存在,并且不存储就不行。
    有没有通用匹配的写法,不要写成www?这种固定的
    试了很多遍都不行,怎么写这段正则啊?

    5 replies    2019-04-08 17:35:35 +08:00
    xiangyuecn
        1
    xiangyuecn  
       Apr 8, 2019
    参考我早上发的 /t/552813,前瞻匹配部分和我以前写法一样,是不会有效果的。另外记录一下:刚才发了一篇吐槽 github 的被水深火热了。。现在被关小黑屋中
    geelaw
        2
    geelaw  
       Apr 8, 2019 via iPhone
    http:(?=([A-Za-z0-9_-]+\.)*(google|sougou)\.)

    用一个 positive lookahead 即可上面这个表达式不能处理 http COLON SLASH SLASH google DOT phishing DOT com 的情况,另外搜索引擎搜狗的域名其实是 sogou。
    1OF7G
        3
    1OF7G  
       Apr 8, 2019
    http(?=:\/\/(www\.)?(google|sougou))

    例子: https://regex101.com/r/Am45LD/1
    蓝色是匹配的内容,只会匹配到 http。其它颜色是分组捕获,可以不用管,或者改成非捕获分组。
    geelaw
        4
    geelaw  
       Apr 8, 2019 via iPhone
    @geelaw #2 忘记 // 了(
    ryd994
        5
    ryd994  
       Apr 8, 2019
    用 named capture,这可靠的多。将来要增加 capture 也可以很方便地加。
    不要用数字序号。
    About     Help     Advertise     Blog     API     FAQ     Solana     2772 Online   Highest 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 36ms UTC 11:48 PVG 19:48 LAX 04:48 JFK 07:48
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86