dongqishou7471 2015-03-20 01:35
浏览 26
已采纳

preg_match_all特殊条件下的子域

Update:

I'm using this regex:

/[^a-z^-^0-9^@^%^\/^:^\.^-]((?<!w\.)(?!w+\.)([0-9a-z][0-9a-z\-]*\.){2,}[a-z]+)(["|\s|<]|$)/i

The regex have a little problem when the string is only domains and nothing else, listed under each other in new lines

For example:

$string = 'sub84.example4.com
sub-example.example84.net
sub-84example.example-h1.org
www-example4124.domain.com
sub.example-www.com';

All domains should be matched, but he current regex is only match sub-example.example84.net and www-example4124.domain.com

I also looking to add some more conditions:

1) letters of domain must be small and can't be capital (the current one doesn't care about that) EX: Sub.example.Com is not ok.

2) No =SPACE (SPACE (" :SPACE before the domain and NO SPACE) or SPACE= ") SPACE: after it.

EX:

$string = '

text = sub1.example.com text
text ( sub2.example.com text
text sub3.example.com = text
text sub4.example.com ) text
text ("sub5.example.com text
text sub6.example.com") text
text : sub7.example.com text
text sub8.example.com : text

';

None of them is ok

3) Exclude .info .biz .tv tlds

Thank you.

  • 写回答

3条回答 默认 最新

  • doulierong0334 2015-03-27 10:43
    关注

    I don't quite understand the purpose of doing this or the incredible number of conditions, but you might try this (rather really long one...):

    (?<!http://)(?<!https://)(?<!www\.)(?![^\s<>]*[:@/])(?<![(=:] )(?<!\(")(?:(?<=[\s">])|^)(?:[a-z0-9-]+\.){2}(?!info|biz|tv)[a-z]+(?=[\s"<]|$)(?! [=):])(?!"\))
    

    regex101 demo


    Breakdown:

    (?<!http://)(?<!https://)(?<!www\.)        # Prevent http:// https:// and www.
    (?![^\s<>]*[:@/])                          # Prevent : @ /
    (?<![(=:] )(?<!\(")                        # Prevent '( ' '= ' etc
    (?:(?<=[\s">])|^)                          # Ensure there's ' ', '>', '"' or beginning of line
    (?:[a-z0-9-]+\.){2}(?!info|biz|tv)[a-z]+   # Main match, alphanumerics and -, 2 parts + tlds (excluding info, biz, tv
    (?=[\s"<]|$)                               # Ensure there's ' ', '<', '"' or end of line
    (?! [=):])(?!"\))                          # Prevent ' )' ' =' etc
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来