Thursday, January 25, 2018

Python for regex search and replace

# import the needed modules (re is for regex)
import os, re

# set the working directory for a shortcut
os.chdir('D:/test')

# open the source file and read it
fh = file('file.txt', 'r')
subject = fh.read()
fh.close()

# create the pattern object. r means the string is send as raw so we don't have to escape our escape characters
pattern = re.compile(r'\(([0-9])*,')
# do the replace
result = pattern.sub("('',", subject)

# write the file
f_out = file('file.txt', 'w')
f_out.write(result)
f_out.close()

Python re.sub Examples

See also Python re.match
Example for re.sub() usage in Python

Syntax

import re result = re.sub(pattern, repl, string, count=0, flags=0);

Simple Examples

num = re.sub(r'abc', '', input) # Delete pattern abc num = re.sub(r'abc', 'def', input) # Replace pattern abc -> def num = re.sub(r'\s+', '\s', input) # Eliminate duplicate whitespaces num = re.sub(r'abc(def)ghi', '\1', input) # Replace a string with a part of itself
 

Python re.match Examples

See also Python re.sub
Note that re.match() matches from the start of the string. Use re.search() when you want to match anywhere in a string.
  • Use re.search() if you want to search anywhere inside a string
  • Use re.sub() if you want to substitute substrings.
  • Use re.split() if you want to extract fields when you have a common field separator.

Syntax

Ad-hoc match

import re result = re.match(pattern, string, flags=0);

Pre-compiled pattern

Use this if you use a pattern multiple times. import re pattern = re.compile('some pattern') result = pattern.match(string [, pos [, end]]);

Simple Examples

result = re.match(r'abc', input) # Check for substring 'abc' result = re.match(r'^\w+$', input) # Ensure string is one word pattern = re.compile('abc') # Same as first example result = pattern.match(input)