mmxool
mmxool

Reputation: 21

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. In the below example it will could be name1, name2 and 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?

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.

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:  

Upvotes: 1

Views: 3967

Answers (1)

Sabuj Hassan
Sabuj Hassan

Reputation: 39385

You can use .* means any character except \n (0 or more times)

or

.+ means 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 . * ( etc

Upvotes: 3

Related Questions