正则表达式 - 如何创建一个正则表达式来检查两个不同长度的字符串,但一个取决于另一个

时间:2022-09-10 14:08:49

If there is a string like this "abcdefghijklm.com 80 /abcdefgh.php" where the domainname followed by http port and the sub string is first 8 digits of the domain name always and followed by ".php" (the sub-string character will change to 6 OR 8 OR 5 at times but however all those length would contain same characters of domain name and endswith .php more examples like this,

如果有一个像这样的字符串“abcdefghijklm.com 80 /abcdefgh.php”,其中域名后跟http端口,子字符串是域名的前8位数字,后跟“.php”(子字符串字符)将有时更改为6 OR 8 OR 5,但是所有这些长度将包含相同的域名字符并以.php结尾更多这样的示例,

xyzklmopqr.com 80 xyzklm.php  
lkjhgfdsaq.com 80 lkjhg.php  
mjuyhnbgtr.com 80 mjuyhnbg.php

3 个解决方案

#1


2  

This works and you can easily change the numbers

这有效,您可以轻松更改数字

(\w{5,6}|\w{8})\w*\.com 80 \1\.php

It's a little simpler than the other guy's solution

这比其他人的解决方案简单一点

#2


1  

The following should work:

以下应该有效:

(((\w{5})\w?)\w{2}?)\w*\.com 80 (\1|\2|\3)\.php

Note that this works for the specific lengths you mentioned in your question (5, 6, and 8), not for any generic length substring.

请注意,这适用于您在问题(5,6和8)中提到的特定长度,而不适用于任何通用长度子字符串。

Example: http://www.rubular.com/r/NwCcihN6o6

示例:http://www.rubular.com/r/NwCcihN6o6

#3


0  

I would try ([a-z]{6})\S* 80 \1\.php

我会尝试([a-z] {6})\ S * 80 \ 1 \ .php

That would work for your 6 case, you can change the number as needed for your other cases.

这适用于您的6个案例,您可以根据需要更改其他案例的数量。

#1


2  

This works and you can easily change the numbers

这有效,您可以轻松更改数字

(\w{5,6}|\w{8})\w*\.com 80 \1\.php

It's a little simpler than the other guy's solution

这比其他人的解决方案简单一点

#2


1  

The following should work:

以下应该有效:

(((\w{5})\w?)\w{2}?)\w*\.com 80 (\1|\2|\3)\.php

Note that this works for the specific lengths you mentioned in your question (5, 6, and 8), not for any generic length substring.

请注意,这适用于您在问题(5,6和8)中提到的特定长度,而不适用于任何通用长度子字符串。

Example: http://www.rubular.com/r/NwCcihN6o6

示例:http://www.rubular.com/r/NwCcihN6o6

#3


0  

I would try ([a-z]{6})\S* 80 \1\.php

我会尝试([a-z] {6})\ S * 80 \ 1 \ .php

That would work for your 6 case, you can change the number as needed for your other cases.

这适用于您的6个案例,您可以根据需要更改其他案例的数量。