Skip to content

Latest commit

History

16 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

replagex

Use replagex to easily replace massive strings on any text using regex.

Install

Currently downloadable from TestPyPI

Example usage

  1. Using JSON file

    example.json:

    {
    "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"
    }
    ]
    }

    Warning

    You have to escape the \ character on the JSON file, like this:
    \d -> \\d

    script.py

    fromreplageximportReplagexrg=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))

    Script Output:

    xxx xxx xxxx
    xxx-xxx-xxxx
    (xxx) xxx xxxx
    xxxxxxxxxx


  1. Without JSON file

    script.py

    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))

    Script Output:

    xxx xxx xxxx
    555-513-4154
    (555) 513 4154
    5555134154


Extra parameters for patterns

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
)
)

About

Use replagex to easily replace massive strings on any text using regex.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Contributors

Languages