简体   繁体   English

正则表达式匹配域名的第一部分

[英]Regex matching first part of domain name

I am trying to match regex for the Fully Qualified Domain Name (fqdn) like this: 我正在尝试将正则表达式匹配为完全合格域名(fqdn),如下所示:

1.test02a-d.test.example.net
2.test02b-d.test.example.net
3.test03a.test.example.net
4.test03b.test.example.net
5.test04-d.test.example.net
6.test05.test.example.net

I tried this regex to get the first part of the fqdn: 我尝试使用此正则表达式来获取fqdn的第一部分:

[^.]+

Criteria: Here I need to get the list of fqdn that has alphabets 'a' or 'b' immediately after number in first part. 准则:在这里,我需要获取第一部分数字后紧跟字母“ a”或“ b”的fqdn列表。 As shown in the above example 1,2,3,4 matches this condition. 如以上示例所示,1,2,3,4匹配此条件。 ie, test02a, test02a-d, test 02b and test02b-d test02a,test02a-d,test 02b和test02b-d

Fqdn 5 and 6 should be ignored as per the above criteria. 根据上述标准,应忽略Fqdn 5和6。

How to modify regex for matching this criteria? 如何修改正则表达式以匹配此条件?

Note: This should be used as REGEXP in Mysql and hence some direct javascript regexes didn't work. 注意:这应该在Mysql中用作REGEXP,因此某些直接的javascript正则表达式不起作用。 The solution should be applicable for both javascript and mysql. 该解决方案应同时适用于javascript和mysql。

[a-z]+[0-9]+[ab](-d){0,1}(.test.example.net)

This Regex matches your first four domain names. 此正则表达式与您的前四个域名匹配。 The "[az]+" could also be specified with "(test)" You can test it on this site: http://regexr.com/ 也可以用“(test)”指定“ [az] +”。您可以在以下站点上对其进行测试: http : //regexr.com/

MySQL version: MySQL版本:

^[^.]+[0-9]+[ab](-[^.]+)?[[:>:]]

JavaScript version: JavaScript版本:

^[^.]+[0-9]+[ab](-[^.]+)?\b

regex101.com doesn't support MySQL flavor regexp, so I only give the JavaScript version . regex101.com不支持MySQL风格的regexp,因此我只提供JavaScript版本 test1bw.test.example.net is added as a test string. 将test1bw.test.example.net添加为测试字符串。

[[:>]] is specific to MySQL. [[:>]]特定于MySQL。 For JavaScript, \\b should be used instead of [[:>]] . 对于JavaScript,应使用\\b而不是[[:>]] [[:>]] is a Zero-Length Assertion matching at the end of a word (position preceded by but not followed by an ASCII letter, digit, or underscore). [[:>]]是单词末尾的零长度断言匹配(位置以ASCII字母,数字或下划线开头,但后面没有)。 \\b matches at a position that is the start or end of a word. \\b在单词开头或结尾的位置匹配。

MySQL doesn't support any shorthand character classes, so [[:digit:]] or [0-9] should be used instead of \\d MySQL不支持任何速记字符类,因此应使用[[:digit:]][0-9]代替\\d

Try 尝试

^[^.]+\d[ab][^.]*

It'll match, from the start of a line ( ^ ) any characters (more than one) not being a dot. 从一行( ^ )开始,它将匹配不是点的任何字符(多个)。 Then there must be a digit followed by an a or a b , Optionally followed by another sequence of characters not being a dot. 然后必须有一个数字,后跟ab ,还可以选择后面跟着另一个不是点的字符序列。

See it here at regex101 . 在regex101上看到它

[a-zA-Z]+[\d]+[a-zA-Z]+.*

workes for me... try it here matches the whole domain. 为我工作... 在这里尝试匹配整个领域。

[a-zA-Z]+[\d]+[a-zA-Z]+[-\w]*

matches only the first part of the domain. 仅匹配域的第一部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM