Reputation: 85
I'm new to Python, i have text file which consists of punctuation and other words how to recompile using specific text match.
text file looks like below actual with more that 100 sentences like below
file.txt
copy() {
foundation.d.k("cloud control")
this.is.a(context),reality, new point {"copy.control.ZOOM_CONTROL", "copy.control.ACTIVITY_CONTROL"},
context control
I just want the output something like this
copy.control.ZOOM_CONTROL
copy.control.ACTIVITY_CONTROL
i coded something like this
file=(./data/.txt)
data=re.compile('copy.control. (.*?)', re.DOTALL | re.IGNORECASE).findall(file)
res= str("|".join(data))
The above regex doesn't match for my required output. please help me on this issue. Thanks in Advance
Upvotes: 1
Views: 804
Reputation: 626929
You need to open and read the file first, then apply the re.findall
method:
data = []
with open('./data/.txt', 'r') as file:
data = re.findall(r'\bcopy\.control\.(\w+)', file.read())
The \bcopy\.control\.(\w+)
regex matches
\bcopy\.control\.
- a copy.control.
string as a whole word (\b
is a word boundary)(\w+)
- Capturing group 1 (the output of re.findall
): 1 or more letters, digits or _
See the regex demo.
Then, you may print the matches:
for m in data:
print(m)
Upvotes: 2