write a regex for compile runtime keyword


write a regex for compile runtime keyword


text = """ Pratap
pandey
age
25
student
"""
keyword = "age"

re_compile = re.compile('((.*n+){2})keyword((.*n+){2})')
re_result = re.findall(re_compile, text)



I want to write a regex for extracting two lines before keyword and two lines after keyword when keyword is matched, with variable.





Would you please add an example?
– andrew
Jun 28 at 9:34





If i try with re_compile = re.compile('((.*n+){2})age((.*n+){2})'), then works, age instead of keyword, but i want to extract with variable name
– Pratap
Jun 28 at 9:35






I mean, an example of input and expected output.
– andrew
Jun 28 at 9:36






I don't think this is possible, once compiled, the regex can't be modified. Instead you can use regex without compiling them before, modifying the string each time you change the keyword. This topic could help you stackoverflow.com/questions/6930982/…
– pLOPeGG
Jun 28 at 9:45






@andrew input: Pratap pandey age 25 student, and if it founds keyword age then print line
– Pratap
Jun 28 at 9:53




3 Answers
3



Possible Solution in Python 2.7



You can use regular expressions uncompiled and put some string formatting in it.


from __future__ import print_function

import re

text = """ Pratap
pandey
age
25
student
"""
keywords = ("age", "else")

for key in keywords :
print(re.findall(r'(.*n+)(.*n+){}n+(.*n+)(.*n+)'.format(key), text))



Output:


[(' Pratapn', 'pandeyn', '25n', 'studentn')]



(*) Edited regular expression.





thanks but didn't work. mine main problem is to extract two lines before keyword and two lines after keyword when keyword is match
– Pratap
Jun 29 at 4:31





@Pratap Then your regular expression may be incorrect... This will be the output of the whole: Two identical lines before a keyword followed by another two identical lines, e.g. "abcdefnabcdefnKEYnghijknghijkn"... if and only if keyword = "KEYn"
– Sven Krüger
2 days ago


"abcdefnabcdefnKEYnghijknghijkn"


keyword = "KEYn"





@Pratap Have a look at my changes.
– Sven Krüger
2 days ago



To match two lines before and after the keyword use a regex like this:


(?:.*(?:r?n)+){2}age(?:.*(?:r?n|$)+){3}



Demo



Explanation:


(?:.*(?:r?n|$)+){3}


age


25



However, since this could be the end of the string, I've added $ as an alternative. I've also added an optional r before n which comes handy if your strings may contain Windows line endings, otherwise remove them.


$


r


n



Sample code:


import re
regex = r"(?:.*(?:r?n)+){2}age(?:.*(?:r?n|$)+){3}"
test_str = (" Pratapn"
"pandeyn"
"agen"
"25n"
"student")

matches = re.finditer(regex, test_str, re.MULTILINE)
for match in matches:
matchNum = matchNum + 1
print (match.group())



I'm not completely sure what you are asking. I think what you are trying to ask is how you put in the value of a variable named "keyword"



This is how you would do that


re.compile(f"(((.*n+){{2}})s*{keyword}s*n((.*n+){{2}}))")



If you define keyword = <some value>, then the code above will work.



Btw. you need to use group 1 when extracting to get what you're looking for.





extract two lines before keyword and two lines after keyword when keyword is match
– Pratap
2 days ago






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

how to run turtle graphics in Colaboratory

Export result set on Dbeaver to CSV