douhan4093 2017-11-29 05:29
浏览 52
已采纳

正则表达式匹配子域

I need a regex that matches all the subdomains of a specific domain. The content media type is unknown (i.g. may be urlencoded, json etc) So far I've got this simple regex re, err := regexp.Compile(`([-a-z0-9])+[.](.*)domain.com`)

The issue is that for a string such this below the whole string is Found instead to get the specific sudomains extracted such prefix1-prefix2.domain.com so the question is what regexp do I need to get only the subdomains without the extra string (i.e %2Fdocument...)

prefix1-prefix2.domain.com%2Fdocument%2F%3Fusp%3Dmkt_docs%26lng%3Den&followup=https%3A%2F%2Fprefix1-prefix2.domain.com%2Fdocument%2F%3Fusp%3Dmkt_docs%26lng%

  • 写回答

2条回答 默认 最新

  • dtmwnqng38644 2017-11-29 05:44
    关注

    The greedy (.*) in your regex is matching all the stuff that you don't want, you can remove that. Also, I escaped your . characters and moved your + to be inside the capture group.

    Change your regex to: ([-a-z0-9]+)\.domain\.com or in the context of your code:

    regexp.Compile(`([-a-z0-9]+)\.domain\.com`)
    

    Here's a regex101 fiddle you can test it with.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办
  • ¥15 vue2登录调用后端接口如何实现