SEARCH W7R

Showing posts with label RegEx. Show all posts
Showing posts with label RegEx. Show all posts

Monday, January 21, 2013

Regex Tutorial 3: Phone Nums


w7r.blogspot.com
Tutorial #2 Answers
  1. "a{3}b{4}c{5}"
  2. "kne{2}"
  3. "cab{2}age"
  4. "2{2}nd stre{2}t"



Typical Regex

Often times, the data we are trying to capture using Regex is not exactly clear. For example, with Regex you may want to capture all of the phone numbers on a website without knowing what actual numbers you will find.

Searching the literal text "333-333-3333" would not be a means of locating all of the phone numbers on a website. It would only capture results with that exact phone number.

So, we need to describe a pattern that follows: 3 digits + hyphen + 3 digits + hyphen + 4 digits.



The Digit Character

"\d" is the text to describe 1 digit. A digit character can only match: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9.
So, "\d\d\d" could match the first three digits of a phone number; however, we can simplify this expression to be "\d{3}". The curly brackets surround the exact number of digits we want to target.
So far "\d{3}" matches the 1st of 5 parts in the phone number pattern we would like to describe. The part following the three digits is a hyphen "-". A hyphen is treated literally in regex because the hyphen has no other purpose in regex besides specifying ranges which appear only in the quantity specifying portion of a regular expression.

Wednesday, January 16, 2013

Regex Tutorial 2: Repeat This, Repeat That

w7r.blogspot.com

Let's Avoid Being Redundant!

Regular Expressions can save you a lot of time, by describing complicated patterns that would be ridiculous to search documents for with a standard Search and Replace.

Repeat Operators

Monday, January 14, 2013

Regex Tutorial 1: Literal Text

w7r.blogspot.com

What is Literal Text?

Literal text is what it sounds like "literal text". Literal text is kind of a WYSIWYG method of capturing data from documents. WYSIWYG stands for What You See is What You Get.

Example #1: Literal Text

Regular Expression Pattern 132
Document text "abc123 321 132 1 3 2 1,32 1321321221"
Findings"abc123 321 132 1 3 2 1,32 1321321221"
Caught text "132", "132", "132"

Importance of Literal Text

20 Resources to Learn Regex

w7r.blogspot.com

The order of the resources is insignificant. These are all great resources for learning Regular Expressions (Regex). All of these resources have been added to the web Resources page under the Regex category.

Saturday, June 23, 2012

Regular Expressions

w7r.blogspot.com

Regex Basics

  • Regex stands for regular expressions
  • Used to describe patterns in text files
  • Can detect dynamic patterns, such as phone numbers followed by a persons name
  • Supported in nearly every programming languages with minimal differences
  • A standard for efficient editing of text documents
  • A more powerful version of "Find and Replace"
  • Can save you a lot of time revising typed up work

Like Find and Replace on Roids

If you have heard of the Find, Replace, or Find and Replace feature on Microsoft Word or any other software, then you probably know that it can eliminate a lot of time spent on tedious and repetitive tasks such as changing every occurance of the word "he" with "she" or "Mr" to "Mrs" or "Ms".

Efficiency Is Key

No matter how large and complex a pattern becomes, the time spent finding/coding the correct regular expression will stay relatively the same. Regex code can be reused and altered to describe similar situations that do not match the original expressions criteria.

The reason Regex is efficient is ultimately because you are allowing a computer to repeat the pattern instead of you, your mouse, your eyes, your keyboard do manually.

An Example of Regex's Efficiency

A blogger, such as myself realizes that he has a lot of <b>bold text</b> tags instead of the preferred <strong>bold text</strong>. It was an easy mistake to make using the "Compose" tab in Blogger's post editor, but a very difficult problem to fix via the HTML tab in post editor.

The blogger spends 30 minutes fixing the format for one of his many posts and realizes he can't waste so much time on such a small detail. So, the next time he fixes the format of a post he utilizes the Find and Replace feature and spends only 8 minutes fixing the format of a post of similar length. He's satisfied, but does not want to waste 8 minutes each on all of his posts (8min/1post, 800min/100posts).

Using Regex the same blogger is able to apply the same edits to each post in 1 minute, because all he has to do is reuse his previously written expression (the same changes occur on each blog post). With practice regex code for these canges can be written in under 5 minutes.

Thus, the blogger could apply all the necessary format changes to his 100 blog posts in under 105 minutes. That is 1 hour and 45 minutes well spent!

Here is a regular expression I used to change my <b> tags to <strong> tags


Find: <(\s*)b((>)|(\s+.*>))
Replace: <strong>
Find: <(\s*)/(\s*)b((>)|(\s+.*>))
Replace: </strong>