Compile-Expressions
# precompile regex patterns
regex_entries = [
re.compile(p)
for p in ['awesome', 'ocean']
]
content = 'Duckduck go is awesome and it is getting better everyday'
print('Text : {!r}'.format(content))
Text : 'Duckduck go is awesome and it is getting better everyday'
for regex in regex_entries:
print('Finding {} -> '.format(regex.pattern), end = ' ')
if(regex.search(content)):
print('matched')
else:
print('not matched')
Finding awesome -> matched
Finding ocean -> not matched
Score: 5
Find-All
title: "Find All"
author: "Raja CSP Raman"
date: 2019-04-20
description: "-"
type: technical_note
draft: false
content = """OSPF Process 1 with Router ID 1.1.1.1
Area: 0.0.0.11
Link State Database
Router test
Maintenance
"""
'OSPF Process 1 with Router ID 1.1.1.1\n Area: 0 …
Read More
Find Multiple Whitespace
title: "Find Multiple Whitespace"
author: "Raja CSP Raman"
date: 2019-04-20
description: "-"
type: technical_note
draft: false
Help on function split in module re:
split(pattern, string, maxsplit=0, flags=0)
Split the source string by the occurrences of the pattern,
returning a list containing the resulting …
Read More
Find Two Digits With Spaces
title: "Find Two Digits with Spaces"
author: "Raja CSP Raman"
date: 2019-04-20
description: "-"
type: technical_note
draft: false
text = "It happened on Feb 21 at 3:30"
answer= re.findall(r'\b\d{2}\b', text)
print(answer)
# To match only two digits with spaces
answers = re …
Read More
Get Full Matches
title: "Get Full Matches"
author: "Raja CSP Raman"
date: 2019-04-20
description: "-"
type: technical_note
draft: false
import re
from typing import List
_RGX = re.compile(r'(.)\1*')
def long_repeat(string: str) -> List[str]:
return [m.group(0) for m in _RGX.finditer(string)]
print(long_repeat('devvvvveeeeeeeeeeeloooooooooper'))
['d', 'e', 'vvvvv', 'eeeeeeeeeee', 'l …
Read More
Get-Specific-Contents
title: "Get Specific Contents"
author: "Raja CSP Raman"
date: 2019-04-20
description: "-"
type: technical_note
draft: false
content = """<!DOCTYPE html>
<!-- The following setting enables collapsible lists -->
<p>
<a href="#human">Human</a></p>
<p class="collapse-section">
<a class="collapsed collapse-toggle" data-toggle="collapse"
href=#mammals>Mammals</a>
<div class="collapse" id="mammals">
<ul …
Read More
Letter-Match
title: "Letter Match"
author: "Raja CSP Raman"
date: 2019-04-20
description: "-"
type: technical_note
draft: false
contents = [
"AB23",
"A2B3",
"DE09",
"MN90",
"XYi9",
"XY90"
]
for content in contents:
regex_pattern = "(?:AB|DE|XY)\d+"
# starts with AB or DE; should contain one or more number
m = re.match(regex_pattern, content)
if(m …
Read More
Negative Lookahead
title: "Negative Lookahead"
author: "Raja CSP Raman"
date: 2019-04-20
description: "-"
type: technical_note
draft: false
test = [ ('i sure like eating pie, but i love donuts', True),
('i sure like eating pie, but i hate donuts', True),
('i sure hate eating pie, but i like donuts', False) ]
Read More
Number-Check
title: "Number Check"
author: "Raja CSP Raman"
date: 2019-04-20
description: "-"
type: technical_note
draft: false
pattern = re.compile("^([0-9]+)+$")
content = "s23434"
if(pattern.match(content)):
print(pattern.match(content).pos)
else:
print('not matched')
content = "23434"
if(pattern.match(content)):
print('matched at : ', pattern.match(content).pos …
Read More
Ontario-Postal-Code
title: "Ontario Postal Code"
author: "Raja CSP Raman"
date: 2019-04-20
description: "-"
type: technical_note
draft: false
names = ['M2N1H5', 'M2N 1H5', 'M882J8']
regex_patten = "^[A-Z]\d[A-Z]\s*\d[A-Z]\d"
# starts with Capital letter; it can have zero or one space
for name in names:
m = re.match(regex_patten, name …
Read More