Python regular expressions

function

Regular Expressions and its function 

RE : used for string matching in python . To understand the RE , Meta Characters are useful and will be used in function of module re. There are 14 meta characters .

Function compile()
used as searching for pattern matches or performing string substitutions.

example :

import
re

p
=
re.
compile
(
'[a-e]'
)

print
(p.findall(
"Aye, said Mr. Gibenson Stark"
))

output :

[‘e’, ‘a’, ‘d’, ‘b’, ‘e’, ‘a’]

Metacharacter backslash ‘\’ has a very important role as it signals various sequences. If the backslash is to be used without its special meaning as metacharcter, use’\\’

Set class [\s,.] will match any whitespace character, ‘,’, or,’.’

example :

import
re
p
=
re.
compile
(
'\w'
)
print
(p.findall(
"He said * in some_lang."
))
p
=
re.
compile
(
'\w+'
)
print
(p.findall(
"I went to him at 11 A.M., he said *** in some_language."
))
p
=
re.
compile
(
'\W'
)
print
(p.findall(
"he said *** in some_language."
))

output :

function split()

Split string by the occurrences of a character or a pattern, upon finding that pattern, the remaining characters from the string are returned as part of the resulting list.
Syntax : re.split(pattern, string, maxsplit=0, flags=0)

pattern :- denotes the regular expression .

string :- pattern search  and  split takes place .

maxsplit :- ‘0’ if not given , and if any nonzero value , then splits take place . If maxsplit = 1,   split takes place  one time only. The flags are very useful and can help to shorten code, they are not necessary parameters.

example :

import
re
print
(re.split(
'\d+'
,
'On 12th Jan 2016, at 11:02 AM'
,
1
))
print
(re.split(
'[a-f]+'
,
'Aey, Boy oh boy, come here'
, flags
=
re.IGNORECASE))
print
(re.split(
'[a-f]+'
,
'Aey, Boy oh boy, come here'
))

output :

Function sub()

Syntax :  re.sub(pattern, repl, string, count=0, flags=0)

sub :-  Sub String .

string :- pattern search is done  .

repl :- when search takes place  sub string pattern is replace  .

count :-  check  number of times this occur.

example :

import
re
print
(re.sub(
'ub'
,
'~*'
,
'Subject has Uber booked already'
, flags
=
re.IGNORECASE))
print
(re.sub(
'ub'
,
'~*'
,
'Subject has Uber booked already'
))
print
(re.sub(
'ub'
,
'~*'
,
'Subject has Uber booked already'
, count
=
1
, flags
=
re.IGNORECASE))
print
(re.sub(r
'\sAND\s'
,
' & '
,
'Baked Beans And Spam'
, flags
=
re.IGNORECASE))

output :

Function subn()

Syntax: re.subn(pattern, repl, string, count=0, flags=0)

subn() is similar to sub() in all ways, except in its way to providing output. It returns a tuple with count of total of replacement and the new string rather than just the string.

example :-

import
re
print
(re.subn(
'ub'
,
'~*'
,
'Subject has Uber booked already'
))
t
=
re.subn(
'ub'
,
'~*'
,
'Subject has Uber booked already'
, flags
 
=
re.IGNORECASE)
print
(t)
print
(
len
(t))
print
(t[
0
])

output :-

Function escape()

Syntax: re.escape(string)

Return string with all non-alphanumerics backslashed, this is useful if you want to match an arbitrary literal string that may have regular expression meta characters in it.

example :

import
re
 
print
(re.escape(
"This is Awseome even 1 AM"
))
print
(re.escape(
"I Asked what is this [a-9], he said \t ^WoW"
))

output :-

Regex Function

  • match :  match regex pattern in  string with option flag. It returns true if a match is found in the string else  return false.
  • search : return match object if match found string.
  • findall : return list having  match of  pattern  string.
  • split : Returns a list in which the string has been split in each match.
  • sub : Replace one or many matches in the string .

 

https://geeksforgeeks.com

https://javatpoint.com

%d bloggers like this: