Reputation: 33
This is what I got:(Edited after blhsing answer.)
import re
File1 = open('text.txt', 'r')
regex = re.compile(r'\b(?:[12]?\d{1,4}|30{4})#[^#]+#')
string = File1.read()
itemdesc = regex.findall(string)
for word in itemdesc:
print (word)
By using: \b(?:[12]?\d{1,4}|30{4})#[^#]+# I could find:
5173#bunch of text here
of, bunch here, text
text here, bunch of
#
After finding this text I would like to replace it in another file where a similar one exists.
At the current stage, I still need to implement something like:
\b(?:number)#[^#]+#
In order to find a text move and replace it in another file where one with the same number is located, also before doing it checking if there are multiple occurrences.
After doing that I will have another problem which is saving the multiple occurrences and storing it in another text in order to manually do the rest.
Hope u guys can help, any help is appreciated it doesn't need to be a solution. :)
Upvotes: 1
Views: 241
Reputation: 106901
The problem here is that you're reading the file and matching the regex line by line when you actually want to match the regex over multiple lines. You should therefore read the entire file into one string before matching it against the regex:
import re
File1 = open('text.txt', 'r')
regex = re.compile(r'\b(?:[12]?\d{1,4}|30{4})#[^#]+#')
string = File1.read()
itemdesc = regex.findall(string)
for word in itemdesc:
print (word)
Upvotes: 2