REGEX & PYTHON

April 7, 2017 | Views: 4195

Begin Learning Cyber Security for FREE Now!

FREE REGISTRATIONAlready a Member Login Here

[/{REGEX} + /{PYTHON}]

Regex or regular expressions can be used to identify a particular pattern in a large dump of data. This comes handy when there is a requirement to extract/identify a particular pattern of data from a heap of data.

USAGE:

  • Log files contain a large amount of data which is not very eye friendly. If we exactly know what we are searching for, regex can help.
  • Post enumeration: picking out emails or phone numbers from a large file in case there are any present

I will be taking a small example on how to identify and extract the phone number from a particular piece of data. Will keep the example straightforward so that it is easy to digest for beginners.

QUESTION 1: what we are lo0king for?

  • Phone numbers (landline)

QUESTION 2: What is the pattern?

  • 011-99999999 (country code – 8 digit landline number)

QUESTION 3: What should be the regex look like?

  • ‘d{3}-d{8}’ -> 3 digits followed by dash and then again 3 digits

Let’s bake the code:

C0de fl0w:

  1. Import re module for regex
  2. Enter the data string. (string starts with what we are searching for)
  3. Enter the second data string. (data to be searched in the middle of the string)
  4. Compile the regex with re.compile function
  5. Use of match function on data to get the data. (starting of string has the phone number pattern)
  6. Use of search function to get the data. (will search for only the first occurrence of the string)
  7. Use of findall function to get the data. (finds all patterns in the string)

py regex

Share with Friends
FacebookTwitterLinkedInEmail
Use Cybytes and
Tip the Author!
Join
Share with Friends
FacebookTwitterLinkedInEmail
Ready to share your knowledge and expertise?
2 Comments
  1. Thanks for sharing

  2. thanks on this

Comment on This

You must be logged in to post a comment.

Our Revolution

We believe Cyber Security training should be free, for everyone, FOREVER. Everyone, everywhere, deserves the OPPORTUNITY to learn, begin and grow a career in this fascinating field. Therefore, Cybrary is a free community where people, companies and training come together to give everyone the ability to collaborate in an open source way that is revolutionizing the cyber security educational experience.

Support Cybrary

Donate Here to Get This Month's Donor Badge

 
Skip to toolbar

We recommend always using caution when following any link

Are you sure you want to continue?

Continue
Cancel