Use replagex to easily replace massive strings on any text using regex.
Currently downloadable from TestPyPI
Using JSON file
{ "patterns": [ { "regex": "(\\d{3} \\d{3} \\d{4})", "replace": "xxx xxx xxxx" }, { "regex": "(\\d{3}-\\d{3}-\\d{4})", "replace": "xxx-xxx-xxxx" }, { "regex": "(\\(\\d{3}\\) \\d{3} \\d{4})", "replace": "(xxx) xxx xxxx" }, { "regex": "(\\d{10})", "replace": "xxxxxxxxxx" } ] }You have to escape the \ character on the JSON file, like this:
\d -> \\dfromreplageximportReplagexrg=Replagex('example.json') # JSON file location# This is just a text example. It could be a text file content.text='''555 513 4154555-513-4154(555) 513 41545555134154'''# Apply regexprint(rg.apply_regex(text))
xxx xxx xxxx
xxx-xxx-xxxx
(xxx) xxx xxxx
xxxxxxxxxx
Without JSON file
fromreplageximportReplagexfromreplagex.patternimportPatternrg=Replagex() # It's just a text example. It could be text file content.text='''555 513 4154555-513-4154(555) 513 41545555134154'''# Add regex pattern using Pattern Class.rg.add_pattern( Pattern( "(\\d{3} \\d{3} \\d{4})", "xxx xxx xxxx" ) ) # Apply regexprint(rg.apply_regex(text))
xxx xxx xxxx
555-513-4154
(555) 513 4154
5555134154
You could use some extra parameters on every regex pattern:
By default are False.
Usage on a JSON file:
{
"regex": "(\\d{3} \\d{3} \\d{4})",
"replace": "xxx xxx xxxx",
"dotall": true,
"ignore_case": true,
"multilne": true
},Using Pattern Class:
rg.add_pattern(
Pattern(
"(\\d{3} \\d{3} \\d{4})",
"xxx xxx xxxx",
dotall=True,
ignore_case=True,
multiline=True
)
)