[英]regex match string followed by any word followed by string variable
I have a predefined variable called probe_name, it is the 3rd word in the string. 我有一个名为probe_name的预定义变量,它是字符串中的第三个单词。 In the below example it will could be name1, name2 and name3
在下面的示例中,它可能是name1,name2和name3
list1 = [
probe url name1
probe http name1
probe tcp name2
probe http name3
abc 123 xyz1
www 123 xyz2 ]
How would I match the lines that match the probe_name value in the list I have above? 如何匹配上面列表中与probe_name值匹配的行?
I want to first match the word "probe" and then need the regex to match any string of characters to follow and then need to match the last word as my probe_value. 我想首先匹配单词“ probe”,然后需要正则表达式匹配要跟随的任何字符串,然后需要匹配最后一个单词作为我的probe_value。
ex: 例如:
probe_name = [name1]
for line in list1:
if ("probe " "regex to match any second word " + probe_name) in line:
what regex can I use to say match any string of characters as the 2nd word? 我可以用什么正则表达式来匹配任何字符串作为第二个单词?
I thought this would have worked but it doesn't: 我以为这会成功,但不会:
if ("probe .* $" + probe_name) in line:
You can use .*
means any character except \\n (0 or more times)
您可以使用
.*
表示any character except \\n (0 or more times)
or 要么
.+
means any character except \\n (1 or more times)
.+
表示any character except \\n (1 or more times)
Example: 例:
if re.match("probe .*"+prob_name, line):
# do something
Make sure the prob_name
doesn't have any special regex character like .
确保
prob_name
没有任何特殊的正则表达式字符,例如.
*
(
etc *
(
等
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.