4
I need to use a regex in the function Find()
in Python for example, if I use inside a loop :
arq = open("arquivo.txt","rb")
var = arq.readline()
a = var.find("abc.*def")
He will be looking at the line "abc something(.*) def" in all lines of the file, beauty
Only now I have two variables containing one string each, I need to call the function Find()
with "Var1 . * Var2", i.e., search for Var1 + anything + Var2
Var1 = "abc"
Var2 = "def"
arq = open("arquivo.txt","rb")
var = arq.readline()
a = var.find(Var1 ".*" Var2) //DESSA FORMA NÃO FUNCIONA
Could someone help me how I can do this kind of search on the line containing regex?
I got the following return: match = re.Compile('{}(.*?){}'.format(var1, var2)). search(var) Valueerror: zero length field name in format, using its code.
– Maurício Salin
I already know the problem, I think you are using python 2.6 or below @Mauríciosalin , I will add a solution for you
– Miguel
My version is 2.6.6 maybe this is the problem, I am not allowed to update, if it is a code in corporate environment.
– Maurício Salin
No problem, we’ll make it work
– Miguel
@Mauríciosalin, test this one: http://ideone.com/F4FsWr
– Miguel
It worked, however you realize that this removing var1 and var2, I need exactly this only that on the contrary, I need to find the line that has var1 . * var2 and print it.
– Maurício Salin
Ha ok then put on "...match.group(0)" @Mauritosalin , or pay only 1 and leave it empty. It’s solved (;
– Miguel
Let’s assume that I have a line "pqrstuvxz" and another line "abc1234556def", I need to check the two lines but print only the one that has abc. *def
– Maurício Salin
That’s right, I had already put . group(0), it was only to make clear, if someone needs the same solution in the future, thanks @Miguel
– Maurício Salin
we had a second problem, when printing match.group(0) theoretically it was to print the whole line, but only imprinting until the second variable
– Maurício Salin
Ha ok, so you’d better put a new question @Mauríciosalin , because here I can’t answer that which is completely different from this question... I’m working on a solution to help you
– Miguel
Let’s go continue this discussion in chat.
– Maurício Salin