Skip to content
Advertisement

Python Regex for Searching pattern in text file

Tags in Sample.txt:

<ServiceRQ>want everything between...</ServiceRQ>

<ServiceRQ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance>want everything between</ServiceRQ> ..

Please can someone help me to get the regex? To extract the expected output from a text file. I want to create a regex to find the above tags. This is what is have tried re.search(r"<(.*?)RQ(.*?)>(.*?)</(.*?)RQ>", line) but not working properly. I want to make a search based on word RQ in text file

The expected output should be

1. <ServiceRQ>want everything between</ServiceRQ>
2. <ServiceRQ> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance>want everything between</ServiceRQ>

Advertisement

Answer

Try this pattern

regex= r'<w+RQ.*?>.*?</w+RQ>'
data=re.findall(regex, line)

The above regex will give output like

['<ServiceRQ>want everything between...</ServiceRQ>', '<ServiceRQ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance>want everything between</ServiceRQ>']

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement