Search code examples
robotframework

How to remove data in Robot Framework using regular expression pattern


I'm want to extract few data without passing it directly like this,

    ${log_line}    Set Variable    {name:abc}[abc@xyz:(pg)]
    ${cleaned_log}    Remove string    ${log_line}    [abc@xyz:(pg)]
    Log ${cleaned_log}

Instead, trying below code with regular expression pattern. But it is not working as expected. Is there any other way? Inputs from anyone would be very helpful.. expected output- {name:abc}

    ${log_line}    Set Variable    {name:abc}[abc@xyz:(pq)]
    ${cleaned_log}    Replace string    ${log_line}    \[.*?\]    ${EMPTY}
    Log ${cleaned_log}

Solution

  • The keyword Replace String Using Regexp can be used instead

    *** Test Cases ***
    Replace using Regexp
        ${log_line}        Set Variable    {name:abc}[abc@xyz:(pg)]
        ${cleaned_log}     Remove String Using Regexp    ${log_line}    \\[.*?\]    ${EMPTY}
        Log                ${cleaned_log}
    

    More information regarding the use of regular expression can be found here: https://robotframework.org/robotframework/latest/libraries/BuiltIn.html#Should%20Match%20Regexp. Hope this helps.