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

Sunday, July 17, 2011

Quick Guide to Regular Expressions

Introducing Regular Expressions – Fastest learning Method

Regular Expressions (or regex) are string patterns which used for text processing. ASP, .NET, C#, Java and many other languages support RegEx. Simply it contains string text and special instructions-statements.
RegEx is a minilanguage that has a special syntax and instructions that you must learn and I like to discuss them step by step.
It is important to note that syntax is the easiest part of mastering regular expressions. The real challenge, however, is learning how to apply that syntax, how to dissect problems into solvable regex solutions. That is something that cannot be taught by simply following exercises, but like any language, mastery comes with practice.


RegEx Lesson 1 – Matching Characters
RegEx Lesson 2 – Matching Several Characters
RegEx Lesson 3 – Repeating Matches

Friday, July 15, 2011

RegEx Lesson 3 – Repeating Matches – Quick Guide to Regular Expressions

3.1 Matching one or more characters
To match one or more instances of a character, simply append a + character.
Eg: matching an email
T: askamith@fmail.com
RegEx: \w+@\w+\.\w+
R: askamith@fmail.com


3.2 Matching Zero or More Characters
use * metacharacter, it is placed right after a character or a set and it will match zero or more instances of the character or set.
T: Amith Ah Amh A3465654747ucrfjerghggh
RegEx: A.*h
R: Amith Ah Amh A3465654747ucrfjerghggh43566dcs

last regex match result reminds us that we have to limit the exact match. its done using (?).


3.2 Matching Zero or One Character - ?
use ? metacharacter for matching zero or one char right after a character.
T: The URL is http://www.askamith.wordpress.com/, to connect
securely use https://www.askamith.wordpress.com/ instead.
RegEx: https?://
R: The URL is http://www.askamith.wordpress.com/, to connect
Securely use https://www.askamith.wordpress.com/ instead.


3.3 Range Interval matching - {n1,n2}
T: 10-6-2004
RegEx: \d{1,2}[-\/]\d{1,2}[-\/]\d{2,4}
R: 10-6-2004


3.4 Preventing Over Matching using Lazy Quantifiers - *?
T: living in <b>SL</b> and <b>LK</b>.
RegEx: <b>.*</b>
R: living in SL and LK.
The reason for this is that metacharacters such as * and + are greedy; they look greatest possible match as opposed to the smallest. You can prevent this by using lazy versions of these quantifiers (they matches the fewest characters instead of the most).
T: living in <b>SL</b> and <b>LK</b>.
RegEx: <b>.*?</b>
R: living in SL and LK.


3.5 Match Word Boundaries
\b is used to match the start or end of a word.
T: The cat scattered his food all over the room.
RegEx: \bcat\b
R: The cat scattered his food all over the room.

RegEx Lesson 2 – Matching Several Characters – Quick Guide to Regular Expressions

2.1 Matching one of Several Characters
T: amith smith gmith
RegEx: [ag]mith
R: amith smith gmith


[Rr]eg[Ee]x ,Match both RegEx or regex.
2.2 Using Character set ranges

    [ns]a[0123456789] == [ns]a[0-9]
    • A-Z matches all uppercase characters from A to Z.
    • a-z matches all lowercase characters from a to z.

    note:
    When you use ranges, be careful not to provide an end range that is less than the start range (such as [Z-A]).

    Multiple ranges may be combined in a single set.
    [A-Za-z0-9] ,This pattern is shorthand for:
    [ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz01234567890]

    Eg: validation RegEx for css color codes;
    T: BGCOLOR="#336633" TEXT="#FFFFFF"
    RegEx: #[0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f]
    R: It will check the color code are in hexadecimal range.
    BGCOLOR="#336633" TEXT="#FFFFFF"


2.3 Anything but not this; (using the ^ metacharacter)
The pattern used in this example is the exact opposite of the one used previously. [0-9] matches all digits (and only digits). [^0-9] matches anything by the specified range of digits. As such,

[ns]a[^0-9]\.xls matches sam.xls but not na1.xls, na2.xls, or sa1.xls.


2.4 Dealing with meta characters with escaping(\):
There may be some special cases where metacharacters wont behave their normal way. For example it cant be used . to match. or [ to match [. So we have to specially pointed out what is the exact role of those meta characters in a Regular Expression.


    T: myArray[0]
    RegEx: myArray\[0\]
    R: myArray[0]


So any metacharacter can be escaped by preceding it with a blackslash (\).


2.5 Matching Whitespace Characters:
When you are performing regular expression searches, you’ll often need to match nonprinting whitespace characters within your text.
Metacharacter Description
[\b] Backspace
\f Form feed
\n Line feed
\r Carriage return
\t Tab
\v Vertical tab
Matching Specific Character types
\d Any digit (same as [0-9])
\D Any nondigit (same as [^0-9])
\w Any alphanumeric character in upper or lower case and underscore (same as [a-zA-Z0-9_])
\W Any nonalphanumeric or underscore character (same as [^a-zA-Z0-9_])
\s Any whitespace character (same as [\f\n\r\t\v])
\S Any nonwhitespace character (same as [^\f\n\r\t\v])

RegEx Lesson 1 – Matching Characters - Quick Guide to Regular Expressions

I want to remind you that RegEx contains plain text and expressions (can contain only plain text). RegEx are highly case sensitive.

1.1 Matching literal text:
Text: Hello, my name is Amith. Welcome
RegEx: Amith
Result: Hello, my name is Amith. Welcome


1.2 For Matching Any Character - .

    T : cat cnt sat
    RegEx: c.t
    R: cat cnt sat

1.3 Matching special characters
A . has a special meaning in regex. If you need a . in your pattern, you need a way to tell regex that you want the actual . character and not the regex special meaning of the . character. To do this, you escape the . by preceding it with a \(backslash) . \is a metacharacter (a fancy way of saying a character with a special meaning, in contrast to the character itself). Therefore, . means match any character, and \. means match the . character itself.

    T : cat cnt.a sat.txt
    RegEx: sat\.txt
    R: cat cnt.a sat.txt