RegexReplace

Format patterns matched with regex. If the regex does not match any pattern, the original value is returned. Example 1: value: user=john regex: user=(.*) output_format: name=\1 -> output value: name=john Example 2: value: xxx=yyy regex: user=(.*) output_format: name=\1 -> output value: xxx=yyy.

python · Filters And Transformers

Details

IDRegexReplace
Languagepython
From Version6.5.0
Docker Imagedemisto/python3:3.12.13.10404775
Tagstransformer string

README

Format patterns matched with regex. If the regex does not match any pattern, the original value is returned.

Example 1:
value: user=john
regex: user=(.*)
output_format: name=\1
-> output value: name=john

Example 2:
value: xxx=yyy
regex: user=(.*)
output_format: name=\1
-> output value: xxx=yyy

Script Data


Name Description
Script Type python3
Tags transformer, string
Cortex XSOAR Version 5.0.0

Inputs


Argument Name Description
value Text to match against
regex Regex pattern to search
output_format Template string to format patterns matched with regex
ignore_case Whether character matching will be case-insensitive. Default is “false”.
multi_line Process value in multiline mode. See more information on re.MULTILINE, see https://docs.python.org/3/library/re.html.
period_matches_newline Whether to make the ‘.’ character also match a new line. Default is “false”.
action_dt The last action for each matched value to transform

Outputs


There are no outputs for this script.

import json

import demistomock as demisto


def side_effect_demisto_dt(obj, dt):
    if dt == ".=val.toUpperCase()":
        return obj.upper()
    elif dt == ".=true":
        return True
    elif dt == ".=null":
        return None
    return None


def test_main(mocker):
    from RegexReplace import main

    with open("./test_data/test-1.json") as f:
        test_list = json.load(f)

    mocker.patch.object(demisto, "dt", side_effect=side_effect_demisto_dt)

    for t in test_list:
        mocker.patch.object(
            demisto,
            "args",
            return_value={
                "value": t.get("value"),
                "regex": t.get("regex"),
                "output_format": t.get("output_format"),
                "ignore_case": t.get("ignore_case"),
                "multi_line": t.get("multi_line"),
                "period_matches_newline": t.get("period_matches_newline"),
                "action_dt": t.get("action_dt"),
            },
        )
        mocker.patch.object(demisto, "results")
        main()
        assert demisto.results.call_count == 1
        results = demisto.results.call_args[0][0]
        assert json.dumps(results) == json.dumps(t["result"])