Python from scratch- Regex

Python from scratch- recap

 
It was 5 weeks ago when I first published my first post.
 
The post was about me learning Python from Scratch.
 
Since then I have also published Part 2 and Part 3 posts.
 
 
To study Python On-Line I am using a Google’s Python class site and I follow it step by step.
 
Each class takes me to read and understand around 2 hours
 
And to do the practice code around 4-5 hours more.
 
The thing is that I do not use these 7 hours in a row but usually over one week.
 
As a fact this is not because I am lazy but because I do it during the night instead of sleeping and when my boys are actually sleeping and not ill.
 
 

Python from scratch- regex

 
Last night I learned Regular Expressions (= RegEx)
 
And boy It was interesting to see how you manage to pull a text out of a large text file.
 
For those of you who don’t know me (or haven’t read my profile yet),
 
I am working in a security company as a QA manager.
 
Our product needs to detect text file leaked out of the perimeter of any organization.
 
The code for the detection is using RegEx over Python
 
And that is a good reason for me to improve my RegEx skills and of course Python.
 
The class itself is confusing.
 
I mean- you know what RegEx is and you certainly understand why someone can use it.
 
But it is damn hard to remember the patterns (and so far they are only the basics)
 
And I also found some illogical parameters here:
  • For some reason, ‘w’ is a word char and ‘s’ is whitespace while in my mind s should be string and w should stand for whitespace. Bizarre.\
  • I would expect Repetition to act like Wildcards but ‘+’ is not a wildcard.
 
Yet, this tool is very powerful and I envy in people controlling it just like our research guys who write these RegEx detection codes.
 

Exercise of regex

 
When I felt ready I came to solve the exercise (linked here)
 
The exercise is long to read but once you see an example of the needed output it becomes easier to understand.
 
So here is my code.
 
I am proud of it and although it looks short it was not short at all in time and I had some tough moments here.
 
I have attached also Google solution which is different than mine.
 
It is ok to be different but what I did take from their solution is how to handle a case of ‘invalid data’ or ‘unexpected data’ unlike my assumption that the data is valid and fits.



Apr/17/2012- Next post here.

8 thoughts on “Python from scratch- Regex”

Leave a Reply