Find for a string in a variable

I need to check if the string ‘sem retorno’ appears in the variable.
I tried to do it this way:

{% macro macro_filtra_fornecedor_por_produto ( codigo_parceiro )  %}

    {% set result_lista_fornecedores %}
        {{ macro_lista_fornecedor ( codigo_parceiro ) }}
    {% endset %}
    
    {% if result_lista_fornecedores|string  like ('%sem retorno%') %}
		{% set parametro_filtro_fornecedor %} 
			and 1 = 1
		{% endset %}
	{% endif %}

    {{ return( parametro_filtro_fornecedor ) }}

    {% endmacro %}

You can use regex in python to find string patterns (or the exact string) About modules variable | dbt Developer Hub

The example provided in the link is

{% set my_string = 's3://example/path' %}
{% set s3_path_pattern = 's3://[a-z0-9-_/]+' %}

{% set re = modules.re %}
{% set is_match = re.match(s3_path_pattern, my_string, re.IGNORECASE) %}
{% if not is_match %}
    {%- do exceptions.raise_compiler_error(
        my_string ~ ' is not a valid s3 path'
    ) -%}
{% endif %}

For your case it would be something like

{% set pattern= 'sem retorno' %}

{% set re = modules.re %}
{% set is_match = re.match(pattern, result_lista_fornecedor, re.IGNORECASE) %}
{{ return( is_match ) }}
{% endif %}
1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.