MatchRegexV2

Extracts regex data from the provided text. The script support groups and looping.

python · Common Scripts

Details

IDMatchRegexV2
Languagepython
From Version5.0.0
Docker Imagedemisto/python3:3.12.13.10404775
TagsUtility

README

Extracts regex data from a given text. This supports groups and looping as well.

Script Data


Name Description
Script Type python3
Tags Utility

Inputs


Argument Name Description
data The text date to extract the regex from.
regex The regex to match and extract. Take into account that data taken from context data contains special characters which are not visible, such as \n and \b.
group The matching group to return. If nothing is provided the full match will be returned. The group value should start at 1.
contextKey The context key to populate with the result.
flags The regex flags to match. The default is “-gim”.

Outputs


Path Description Type
MatchRegex.results List of Regex matches string
import re

import pytest
from MatchRegexV2 import main, parse_regex_flags


class TestParseRegexFlags:
    @staticmethod
    def test__sanity():
        flags, multiple_matches = parse_regex_flags()
        assert re.IGNORECASE in flags
        assert re.MULTILINE in flags
        assert multiple_matches

    @staticmethod
    def test__custom_flags():
        flags, multiple_matches = parse_regex_flags("gi")
        assert re.IGNORECASE in flags
        assert re.MULTILINE not in flags
        assert multiple_matches

    @staticmethod
    def test__custom_flags_without_multiple_match():
        flags, multiple_matches = parse_regex_flags("i")
        assert re.IGNORECASE in flags
        assert re.MULTILINE not in flags
        assert not multiple_matches

    @staticmethod
    def test__invalid_flags():
        with pytest.raises(ValueError):
            parse_regex_flags("gimh")


class TestMain:
    @staticmethod
    def test__sanity():
        args = {
            "data": 'hello world\nthis is a regex test named "sanity". please consider test named "sanity2".',
            "regex": 'test.*"(.*?)"',
        }
        results = main(args)
        assert not results.outputs
        assert 'test named "sanity"' in results.raw_response
        assert 'test named "sanity2"' in results.raw_response

    @staticmethod
    def test__with_unicode():
        args = {
            "data": 'hello world\nthis is a regex test named "sanity". please consider test named "שלום".',
            "regex": 'test.*"(.*?)"',
        }
        results = main(args)
        assert not results.outputs
        assert 'test named "sanity"' in results.raw_response
        assert 'test named "שלום"' in results.raw_response

    @staticmethod
    def test__multiple_with_group():
        args = {
            "data": 'hello world\nthis is a regex test named "sanity".\nplease consider test named "sanity2".\n',
            "regex": 'test.*"(.*?)".$',
            "group": "1",
        }
        results = main(args)
        assert not results.outputs
        assert "sanity" in results.raw_response
        assert "sanity2" in results.raw_response

    @staticmethod
    def test__single_with_group():
        args = {
            "data": 'hello world\nthis is a regex test named "sanity".\nplease consider test named "sanity2".\n',
            "regex": 'test.*"(.*?)".$',
            "group": "1",
            "flags": "im",
        }
        results = main(args)
        assert not results.outputs
        assert "sanity" in results.raw_response
        assert "sanity2" not in results.raw_response

    @staticmethod
    def test__with_high_group():
        args = {
            "data": 'hello world\nthis is a regex test named "sanity". please consider test named "sanity2".',
            "regex": 'test.*"(.*?)"',
            "group": "5",
        }
        results = main(args)
        assert not results.outputs
        assert 'test named "sanity"' in results.raw_response
        assert 'test named "sanity2"' in results.raw_response

    @staticmethod
    def test__with_invalid_group():
        args = {
            "data": 'hello world\nthis is a regex test named "sanity". please consider test named "sanity2".',
            "regex": 'test.*"(.*?)"',
            "group": "not a number",
        }
        with pytest.raises(ValueError):
            main(args)