Sunday, June 21, 2015

Learning Regular Expression (regex) with tool

Regular expression is a way to find pattern in text. It is very useful to search thorough out the text for certain pattern. But it is also not easy to remember it rule and grammar.

I- Some rules and grammars

Metacharacter
Name
Matches
.
[···]
[^···]
dot
character class
negated character class
any one character
any character listed
any character not listed
^
$
\<
\>
caret
dollar
backslash less-than
backslash greater-than
the position at the start of the line
the position at the end of the line
the position at the start of a word
the position at the end of a word
|
(···)
or; bar
parentheses
matches either expression it separates
used to limit scope of , plus additional uses yet to be discussed



Minimum Required
Maximum to Try
Meaning
?
*
+
none
none
1
1
no limit
no limit
one allowed; none required ("one optional")
unlimited allowed; none required ("any amount okay")
unlimited allowed; one required ("at least one")

- [A-Z] or [a-z] : match A-Z or a-z
- [0-9]: match 0-9
- \n : newline
- \t : tab
- \s : space

II- Examples

Example 1:
****Sample in the text:
Jeffrey or Jeffery


**** Regexp:
Jeff(rey|ery)
or
Jeff(re|er)y

2- this regexp is for matching email {[A-Za-z0-9_.-]+@([A-Za-z0-9_]+\.)+[A-Za-z]{2,4}}
it can be user1@gmail.com, admin@yahoo.com or any that follow email address rule

3- this regexp is for matching time ([01][0-9]|2[0-3]):[0-5][0-9]

match from 00:00 to 23:59

4- finding .iso file, the regexp would be \.iso$

III- using added program
visual-regexp is a program aiding you to correct your regexp
- to install use command $ sudo apt-get install visual-regexp

run the application, and use regexp to match email address from text
see picture





we see all the emails are matched, other pattern is ignored


No comments:

Post a Comment