Skip to main content

Python Regular Expressions

Python Regular Expressions
Regular expressions are a very useful technique in extracting information from text such as code, spreadsheets, documents or log-files. The first thing to keep in mind while implementing regular expression is that everything essentially needs to be a character & programmers write patterns to match a specific sequence of characters/strings.

Defining Regular Expression

Regular expressions are characters in special order that help programmers find other sequence of characters or strings or set of strings using specialized syntax held in a pattern. Python supports regular expressions through the standard Python library – ‘re’ which is packed with every Python installation.
Here, we will be learning about the vital functions that are used to handle regular expressions. There are many characters having special meaning when they are used as regular expressions. This is mostly used in UNIX.

Raw Strings In Python

It is recommended to use raw-strings instead of regular strings. When programmers write regular expressions in Python, they begin raw strings with a special prefix ‘r’ and backslashes and special meta-characters in the string, that allows us to pass through them to regular-expression-engine directly.

match Function

This method is used to test whether a regular expression matches a specific string in Python. The re.match(). The function returns ‘none’ of the pattern doesn’t match or includes additional information about which part of the string the match was found.
Syntax:
re.match (pattern, string, flags=0)
Here, all the parts are explained below:
  • match(): is a method
  • pattern: this is the regular expression that uses meta-characters to describe what strings can be matched.
  • string: is used to search & match the pattern at the string’s initiation.
  • flags: programmers can identify different flags using bitwise operator ‘|’ (OR)
Example:
#!/usr/bin/python

import re #simple structure of re.match()
matchObject
= re.match(pattern, input_str, flags=0)
A Program by USING re.match:
Example:
#!/usr/bin/python

import re
list
= [ "mouse", "cat", "dog", "no-match"]
# Loop starts here
for elements in list:
m
= re.match("(d\w+) \W(d/w+)" , element)
# Check for matching
if m:
print (m . groups ( ) )
In the above example the pattern uses meta-character to describe what strings it can match. Here ‘\w’ means word-character & + (plus) symbol denotes one-or-more.
Most of the regular expressions’ control technique comes to role when “patterns” are used.

search Function

It works in a different manner than that of match. Though both of them uses pattern; but ‘search’ attempts this at all possible starting points in the string. It scans through the input string and tries to match at any location.
Syntax:
re.search( pattern, strings, flags=0)
Program to show how it is used:
#!/usr/bin/python
import re
value = "cyberdyne"
g
= re.search("(dy.*)", value)
if g:
print("search: " g.group(1))
s
= re.match("(vi.*)", value)
if s:
print("match:", m.group(1))
Output:
dyne

split Function

The re.split() accepts a pattern that specifies the delimiter. Using this, we can match pattern & separate text data. ‘split()” is also available directly on string & handles no regular expression.
Program to show how to use split():
Example:
#!/usr/bin/python
import re
value = "two 2 four 4 six 6"
#separate those non-digit characters
res
= re.split ("\D+" , value)
# print the result
for elements in res :
print (element)
Output:
2
4
6

In the above program, \D+ represents one or more non-digit characters.

Comments

For Programs Click Here

Popular posts from this blog

Syllabus

Syllabus  C Programming Tutorials C Tutorial C Introduction History of C Programming Language C Installation C Program Structure C Input and Output (I/O) C Format Specifiers Declaration & Assignments C Tokens C Identifiers C Keywords C Constants C Operators C Data Types C Variables C Preprocessors C Type Casting C Custom Header File Flow Control C Decision Making C if Statements C if-else Statements C Nested if-else Statements C else-if Statements C goto Statement C switch Statements C Loops C while loops C do while loops C for loops Functions C Functions C Function Arguments C Library Functions C Variable Scope Arrays & Strings C Arrays C Strings Pointers C Pointers C Dynamic Memory Allocation Structure & Union C Structures C Unions File I/O C File Handling C fopen C fclose C getc C putc C getw C putw C fprintf C fscanf C fgets C fputs C feof                                     ...

Syllabus

Python Tutorials Python Tutorial Python Overview Python Installation Basics of Python Programming Python Operators Python Keywords Python Numbers Python Strings Python Data Types Python Variables Python Lists Python Tuples Python Date and Time Python Decision Making Python Loops Python File Handling Python Dictionaries Python Functions Python Modules Python Exceptions Handling Python Object Oriented Inheritance in Python Python Regular Expressions Python Networking Programming Python Multithreaded Programming Python CGI Programming Python Database Connection Python Metaprogramming Python Data Processing And Encoding Python GUI Programming

C++ Data Abstraction

C++ Data Abstraction Object oriented programming offers various features to write programs with various concepts that help to minimize problems and increase flexibility in program. One of the features of object oriented programming is Data abstraction. In this chapter you will learn about how the concept data abstraction is carried out within the C++ program. What is Data abstraction? Data abstraction allows a program to ignore the details of how a data type is represented. Abstraction (derived from a Latin word  abs , meaning away from and  trahere , meaning to draw) refers to the act of representing essential features without including the background details or explanations. C++ classes use the technique of abstraction and are defined as a list of abstract attributes such as width, cost, size etc and functions to operate on these attributes. They put in a nutshell all the essential properties of an object that are required to be created. The attributes are therefore called...