ExtractInbetween

Extract a string from an existing string.

python · Filters And Transformers

Details

IDExtractInbetween
Languagepython
From Version5.5.0
Docker Imagedemisto/python3:3.12.13.10404775
Tagstransformer string Utility

README

Extract strings from in between other strings using a single command.

  • If the start or end input is a single character, the script looks for the first and last instances,
    respectively, and returns the string that is in between the two instances.
  • If the start or end input is greater than a single character, regex is used to find the first and
    last instances, repectively, and returns the string that is in between the two instances.

Script Data


Name Description
Script Type python
Tags transform, general, Utility

Inputs


Argument Name Description
value The value to extract the data from.
start The string from which to start extracting.
end The string to which to stop extracting.

Outputs


None

import demistomock as demisto  # noqa: F401
import pytest
from CommonServerPython import *  # noqa: F401
from ExtractInbetween import extract_inbetween


def test_extract_inbetween():
    """
    Given:
        A string with specific characters

    When:
        Execute command extract_inbetween

    Then:
        Validate the right output returns.
    """

    # Test 1
    value = "<This is a value>"
    start = "<"
    end = ">"
    res = extract_inbetween(value, start, end)
    assert res == "This is a value"


@pytest.mark.parametrize("value", [{"key1": "value1", "key2": "value2"}, 10])
def test_extract_inbetween_on_invalid_input_types(mocker, value):
    """
    Given:
        a value which is not a string type

    When:
        Execute command extract_inbetween

    Then:
        Validate that return error is raised.
    """
    mocker.patch("CommonServerPython.return_error", return_value=Exception("ERROR: The input value must be a string"))
    with pytest.raises(Exception, match="ERROR: The input value must be a string"):
        extract_inbetween(value, "<", ">")