970_90

Thursday, 19 July 2012

Regular Expression

Regular Expression


Following are some Basic terms of Regular Expression which we should be aware.

1. literal: It is Character(s) OR words which we want to search in regular expression.
For Example:
                we want to search "tutorial" in the "php-tutorial-php.blogspot.in".
                Here "tutorial" is literal.


2. Meta Character: It is special chacter(s) which have special meaning in Regular Expression.
For Example:
                *, ., /, \, ? and ^.


3. Target string: The string in which we want to search the string.
For Example:
                we want to search "tutorial" in the "php-tutorial-php.blogspot.in".
                Here "php-tutorial-php.blogspot.in" is target string.


4. Regular expression: The regex with which we find the string.
For Example:
                we want to search "tutorial" in the "php-tutorial-php.blogspot.in" with regex    /(tutorial)/
                Here "/(tutorial)/" is Regular Expression.





Following are the Regular Expression with matches tested for PHP only.


An Introduction of Regular Expression


In computing, a regular expression provides a concise and flexible means to "match" (specify and recognize) strings of text, such as particular characters, words, or patterns of characters. Common abbreviations for "regular expression" include regexp and regex.
The concept of regular expressions was first popularized by utilities provided by Unix distributions, in particular the editor ed and the filter grep.
Regular Expression  Matche
^A
"A" at the beginning of a line
A$
"A"  at the end of a line
A\^
"A^" anywhere on a line (slash for escape spec. mean of ^) 
\$A
"$A" anywhere on a line
^\^
"^" at the beginning of a line
\$$
"$" at the end of a line
[0]
The character "0"
[0-9]
Any number
[^0-9]
Any character other than a number
[-0-9]
Any number or  "-"
[0-9-]
Any number or  "-"
[^-0-9]
Any character except a number or  "-"
[0-9]\]
Any number followed by a "]"
[0-9-z]
Any number, or any character between 0-9  and "z"
[0-9\-a\]\]
Any number, or - or a
\*
Any line with an asterisk
\\
Any line with a backslash
^\*
Any line starting with an asterisk
^A\*
Any line starting with an "A*"
^AA\*
Any line if it starts with one "AA*"
^A(A)*B
Any line with one or more "A"'s followed by a "B"
e.g.
AB, AAB, AAAB, AAAB
^A{4,8}B
Any line starting with "A" having 4-8 char and followed by B
eg
AAAAB, AAAAAAB, AAAAAAB
^A{4,}B
Any line starting with "A" min 4 character and followed by B
^A{4}B
Any line starting with "AAAAB"
A{4,8}
Any line with "A{4,8}"



1 comment:

  1. The regex <.+> will match first in This is a first test.

    Above will best to search with tags

    ReplyDelete