-2

I'm working on python script that will extract some words. These words are from a larger text and must be after a given strings and some lines.

Like this: Country --> This is the word I know Judecatoria X/Tribunalul X/Judecatoria Sectorului X --> One of these words are present in the text, however I do not know which of them and I want regex to matched it.

import re

text:"""Country  
     Judecatoria Sectorului X"""

#pattern = this is what I am looking for

expected result:['Judecatoria Sectorului X']

Thank you very much!

  • You have mentioned twice what your text is. So its hard to know what the text string is ? Is it the one in your code or the one you mention in description above code? – shuberman May 23 '20 at 08:09

2 Answers2

0

Try this,

>>> import re
>>> text = """Country  
...      Judecatoria Sectorului X"""

>>> [x.strip() for x in re.findall("\n.*", text)]
['Judecatoria Sectorului X']

sushanth
  • 8,275
  • 3
  • 17
  • 28
0

Not sure if I understand the problem correctly, but, if you want to find what is in next line after Country, this should do it:

import re
text = """Country  
     Judecatoria Sectorului X"""
result = re.search('Country\s*\n\s*(.*)\s*', text).group(1)
print(result)

If Country is just an example and it might be one of many words, change Country to (?:Country|Something else|Yet another one)

Błotosmętek
  • 12,717
  • 19
  • 29