简体   繁体   English

正则表达式匹配字符串,后跟任何单词,后跟字符串变量

[英]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.

相关问题 Python Regex:匹配前面或后面没有带数字的单词的字符串 - Python Regex: Match a string not preceded by or followed by a word with digits in it 正则表达式:匹配字符串后跟点/逗号后跟空格 - Regex: match string followed by dot/comma followed by space python regex匹配数字,后跟字符串或不包含任何内容 - python regex match number followed by string or nothing 正则表达式匹配单词,如果可选后跟任何单词,除非后跟某些单词 - Regex match word if optionally followed by any word unless followed by certain words 正则表达式提取多个符号,后跟字符串中的单词-python - regex to extract multiple symbols followed by word in a string -python 如何在字符串中搜索后跟特定单词的任何数字? - How to search a string for any number followed by a specific word? 正则表达式匹配字符串,其中单词后跟空格,然后是数字点或连字符,单词后跟空格,然后(一些信息) - Regex to match string which has words followed by whitespace then digits dot or hyphen and words followed by space and then (some info) 正则表达式检查字符串是否有任何数字后跟 python 中的单位并修改它 - Regex check if a string has any numbers followed by units in python and modify it 仅使用字符串方法将字符串后跟数字匹配 - match a string followed by a number with string methods only 如果后面没有模式,则正则表达式匹配 - regex match if not followed by pattern
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM