From assignment 2, I first came upon the jargon regular expression (regex). Regex is a sequence of characters that forms a search pattern, and is commonly used for pattern matching in strings.
I also recently found out what it means to scrub websites, which is just extracting all the information that you want to do whatever you want with it. And with regex, this is done quite easily!
For example, say you have a mountain of documents and you wanted to extract every street address from it. Doing this manually might take you several hours, but using regex it's a pretty simple matter.
If given that every house number is less than 5 digits in length, with the street name being exactly one word, followed by the word street or avenue and a period always following.
You can use the regular expression
\d{1,5} \s \w+ \s \w+ \.
where
\d{1, 5} #represents digits 1 to 5 in length
\s #followed by a space between
\w+ #w represents anything that's a character, plus sign represents one or more
\s #space again
\w+ #series of characters
\. #period
All regular expressions (as well as special characters) is preceded by a backslash!
There are many uses for regex and I look forward to learning more about it in future CSC courses!
No comments:
Post a Comment