Skip to content

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

License

Notifications You must be signed in to change notification settings

aviloncho/replagex

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

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

    from replagex import Replagex
    
    rg = Replagex('example.json') # JSON file location
    
    # This is just a text example. It could be a text file content.
    text = '''
    555 513 4154
    555-513-4154
    (555) 513 4154
    5555134154
    '''
    
    # Apply regex
    print(rg.apply_regex(text))

    Script Output:

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


  1. Without JSON file

    script.py

    from replagex import Replagex
    from replagex.pattern import Pattern
    
    rg = Replagex()
    
    # It's just a text example. It could be text file content.
    text = '''
    555 513 4154
    555-513-4154
    (555) 513 4154
    5555134154
    '''
    
    # Add regex pattern using Pattern Class.
    rg.add_pattern(
        Pattern(
            "(\\d{3} \\d{3} \\d{4})",
            "xxx xxx xxxx"
        )
    )
    
    # Apply regex
    print(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

License

Stars

Watchers

Forks

Languages