[Tutor] Re Module
Avi Gross
avigross at verizon.net
Thu Dec 27 23:07:10 EST 2018
Asad,
After reading replies to you by Alan and Steven I want to ask you if you can
first tell us in normal words what the exact outline of the program does. If
you only want help on one small part, tell us about that.
I was first fooled into thinking you wanted to show us how you solve the
majority of the entire problem, whatever it was so I wanted to hear things
like I show next.
An example would be to search two files for error matches of various kinds
and report if they contain any matches. Just report True versus False or
something.
Another goal might be to show the first match in some way then quit.
Another might be to do the same search in two files and report ALL the
matches in some format.
After being clear on the goal, you might specify the overall algorithm you
want to use. For example, do you process one file to completion and save
some results then process the other the same way then compare and produce
output? Or do you process both nearly simultaneously in one pass, or perhaps
multiple passes. Do you search for one error type at a time or all at once?
Can there be multiple errors on the same line of the same kind or different
ones? What does error even mean? Is it something like "Fail: 666" versus
"Warn: 42" or something where multiple errors share a part or ...
Once we have some idea of the goal, we could help you see if the approach
seems reasonable even before reading the code. And, when reading the code,
we might see if your implementation seems to match the plan so perhaps we
can see where you diverge from it perhaps with a mistake.
If I just look at what you provided, you do some of what I asked. You are
not clear on what the two files contain other than they may have an error
that you can identify with a set of patterns. Can you tell us if you are
looking at one line at a time, assuming it is a text file? Your code shows
no evidence of a file at all. Your focus in what you share with us is mainly
on creating a list of compiled search patterns and applying it to one
uninitialized "st" and trying to figure out which one matched.
You do not show any examples of the pattern but suggest something is
failing. For all we know one of your patterns just matched the presence of a
single common character or even was not formatted properly and failed to be
compiled.
My impression is you are not actually asking about the overall problem. Your
real question may be how to use a regular expression on a string and find
out what matched. If so, that would be the headline, not about two files.
And it may even be your entire approach could change. An example would be to
store your patterns as a text keyword in a dictionary with the value being
the compiled version so when you evaluate a line using the pattern, you know
which one you matched with. I am NOT saying this is a good solution or a
better one. I am asking you to think what you will need and what techniques
might make life easier in doing it.
So besides trying to alter some code based of the feedback, from others,
could you resubmit the question with a focus on what you are doing and what
exactly is not working that you want looked at. Specifics would be useful
including at least one pattern and a line of sample text that should be
matched by the pattern as an example and perhaps one that should not. And
any error messages are vital.
When you do, I am sure Steven and Alan and others might be able to zoom
right in and help you diagnose, if you don't figure it out by yourself first
by being able to see what your goal is and perhaps doing a little debugging.
-----Original Message-----
From: Tutor <tutor-bounces+avigross=verizon.net at python.org> On Behalf Of
Asad
Sent: Thursday, December 27, 2018 10:10 AM
To: tutor at python.org
Subject: [Tutor] Re Module
Hi All ,
I trying find a solution for my script , I have two files :
file1 - I need a search a error say x if the error matches
Look for the same error x in other file 2
Here is the code :
I have 10 different patterns therefore I used list comprehension and
compiling the pattern so I loop over and find the exact pattern matching
re_comp1 = [re.compile(pattern) for pattern in str1]
for pat in re_comp1:
if pat.search(st,re.IGNORECASE):
x = pat.pattern
print x ===> here it gives the expected output it correct
match
print type(x)
<type 'unicode'>
if re.search('x', line, re.IGNORECASE) is not None: ===> Gives a wrong
match
print line
Instead if I use :
if re.search(x, line, re.IGNORECASE) is not None: then no match occurs
print line
Please advice where I going wrong or what can be done to make it better .
Thanks,
--
Asad Hasan
+91 9582111698
_______________________________________________
Tutor maillist - Tutor at python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
More information about the Tutor
mailing list