regex 正lookbehind和正lookahead正则表达式中的错误- python

6tqwzwtp  于 2022-11-18  发布在  Python
关注(0)|答案(1)|浏览(110)

我有下面的正则表达式
(?i)(?<=\b(?:host)\s*(?:name)\s*[-|:|=|\s]\s*)(?=.*[^\s][\d\_\-\.].*)([a-zA-Z0-9\(\)\._\-\'\"]{5,})
这里我尝试在正则表达式中查找主机名值。我的要求是:
1.主机名中应包含数字、_、-或.
1.主机名可以由字母数字(). _ - '“组成
1.主机名的模式为
hostname: test123hostname= test123hostname- test123hostname test123
host name: test123host name= test123host name- test123host name test123
hostname : test123hostname = test123hostname - test123hostname test123
因此,这就是为什么我选择了上面提到的正则表达式,以包括积极的lookbehind和积极的lookaheadAssert
我举两个例子:
输入1:host name : en.wikipedia.org is a correct example
输入2:Verify that the hostname installation configures the system.
输入1是正确的示例,因为检测到的字符串en.wikipedia.org包含.,而输入2中installation的检测是无效的检测,因为不包含数字或_或。
但是上面的正则表达式a .在Input 2中的句子末尾,该正则表达式匹配。
如何解决这个问题呢?

lyr7nygr

lyr7nygr1#

我不知道你是否解决了你的问题,但这可以为你工作:

host\s?name\s?[:|=|\-|\s]\s?(?=.*)(?=[a-zA-Z]*\d|[a-zA-Z]*[.-_])([\w.-]+[a-zA-Z0-9])(?=\s|$)

请参阅demo here
您将需要使用组来从匹配项中获取主机名。

相关问题