Compile-Expressions

Sat 17 May 2025
import re
# 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

Category: regex


Find-All

Sat 17 May 2025

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
"""
content
'OSPF Process 1 with Router ID 1.1.1.1\n                       Area: 0 …

Category: regex

Read More

Find Multiple Whitespace

Sat 17 May 2025

title: "Find Multiple Whitespace" author: "Raja CSP Raman" date: 2019-04-20 description: "-" type: technical_note draft: false


import re
help(re.split)
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 …

Category: regex

Read More

Find Two Digits With Spaces

Sat 17 May 2025

title: "Find Two Digits with Spaces" author: "Raja CSP Raman" date: 2019-04-20 description: "-" type: technical_note draft: false


import re
text = "It happened on Feb 21 at 3:30"

answer= re.findall(r'\b\d{2}\b', text)
print(answer)
['21', '30']
# To match only two digits with spaces
answers = re …

Category: regex

Read More

Get Full Matches

Sat 17 May 2025

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 …

Category: regex

Read More

Get-Specific-Contents

Sat 17 May 2025

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 …

Category: regex

Read More

Letter-Match

Sat 17 May 2025

title: "Letter Match" author: "Raja CSP Raman" date: 2019-04-20 description: "-" type: technical_note draft: false


import re
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 …

Category: regex

Read More

Negative Lookahead

Sat 17 May 2025

title: "Negative Lookahead" author: "Raja CSP Raman" date: 2019-04-20 description: "-" type: technical_note draft: false


import re
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) ]
rx = re.compile …

Category: regex

Read More

Number-Check

Sat 17 May 2025

title: "Number Check" author: "Raja CSP Raman" date: 2019-04-20 description: "-" type: technical_note draft: false


import re
pattern = re.compile("^([0-9]+)+$")

content  = "s23434"

if(pattern.match(content)):
    print(pattern.match(content).pos)
else:
    print('not matched')
not matched
content  = "23434"

if(pattern.match(content)):
    print('matched at : ', pattern.match(content).pos …

Category: regex

Read More

Ontario-Postal-Code

Sat 17 May 2025

title: "Ontario Postal Code" author: "Raja CSP Raman" date: 2019-04-20 description: "-" type: technical_note draft: false


import re
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 …

Category: regex

Read More
Page 1 of 2

Next »