Hi, In this blog we will learn all about regular expression.
Now a day regular expression is include most of project framework to connect urls so its important know all about regular expressions. So to solve this and provide best help here we will try to give complete study package of regular expression with example so that you can apply it easily at any where in python.
In python there are 14 metacharacters which used in reg. which we listed below:
And use some Unicode string patterns to find string as per these basis :
\d : Matches any decimal digit; this is equivalent to the class [0-9].
\D : Matches any non-digit character; this is equivalent to the class [^0-9]
\s : Matches any whitespace character; this is equivalent to the class [ \t\n\r\f\v].
\S : Matches any non-whitespace character; this is equivalent to the class [^ \t\n\r\f\v].
\w : Matches any alphanumeric character; this is equivalent to the class [a-zA-Z0-9_].
\W : Matches any non-alphanumeric character; this is equivalent to the class [^a-zA-Z0-9_].
Let's Start with some examples
First need to import it by using this :
>>> import re
Here list some important methods which used to search string patterns
We can combine a regular expression pattern into pattern objects, which can be used for pattern matching. It also helps to search a pattern again without rewriting it.
This will split at every space that is followed by a string of upper-case letters which end in a word-boundary.
The re.sub() function in the re module can be used to replace substrings.
The syntax for re.sub() is re.sub(pattern,repl,string).
That will replace the matches in string with repl.
This function is similar to sub() in all ways except the way in which it provides the output. It returns a tuple with count of total of all the replacements as well as the new string.
Return string with all non-alphanumerics backslashes; this is useful if you want to match an arbitrary literal string that may have regular expression metacharacters in it.
Scan through string looking for the first location where the regular expression pattern produces a match, and return a corresponding match object. Return None if no position in the string matches the pattern; note that this is different from finding a zero-length match at some point in the string.
The match function is used to match the RE pattern to string with optional flags. In this method, the expression "w+" and "\W" will match the words starting with letter 'g' and thereafter, anything which is not started with 'g' is not identified.
The expression re.findall() returns all the non-overlapping matches of patterns in a string as a list of strings.
If you like Codersarts blog and looking for Assignment help,Project help, Programming tutors help and suggestion you can send mail at email@example.com.
Please write your suggestion in comment section below if you find anything incorrect in this blog post