BuiltIn - Should Match Regexp - Unexpected Bad Escape Errorr

Hi

Currently, I’m getting problem to match two strings using Should Match Regexp when one of than (or both) has the sub-string “\R” or “\P” like C:\Retail\Software\POSusb\ or C:\Program Files\WNPOS\ .

Someone have idea how could I match the following strings without bad scape error?

CODE:

#bad_escape_sample.robot

*** Settings ***

Library          BuiltIn


*** Test Cases ***
Bad Scape R

    ${string_1}=   Set Variable  "prefixesC:\\Retail\\Software\\POSusb\\postfixes"
    ${string_2}=   Set Variable  "[\x00-\x7F]*C:\\Retail\\Software\\POSusb\\[\x00-\x7F]*"
    Should Match Regexp   ${string_1}  ${string_2}

Bad Scape P

    ${string_1}=   Set Variable  "prefixesC:\\Program Files\\WNPOS\\postfixes"
    ${string_2}=   Set Variable  "[\x00-\x7F]*C:\\Program Files\\WNPOS\\[\x00-\x7F]*"
    Should Match Regexp   ${string_1}  ${string_2}

RESULT:
image

Hi Jefferson,

I think you may need to escape the escape characters i.e.

should be

${string_2}=   Set Variable  "[\\x00-\\x7F]*C:\\\\Retail\\\\Software\\\\POSusb\\\\[\\x00-\\x7F]*"

The reason is escaped once for the string and again for the regex, this is a common problem in many testing tools and is not unique to Robot Framework

Hope that helps,

Dave.

1 Like

Thank you very much Dave :ninja:. The problem was solved after following your solution. :smiley:

#bad_escape_sample_solved.robot

*** Settings ***

Library          BuiltIn


*** Test Cases ***
Bad Scape R - damies13 Dave Solution

    ${string_1}=   Set Variable  "prefixesC:\\Retail\\Software\\POSusb\\postfixes"
    ${string_2}=   Set Variable  "[\\x00-\\x7F]*C:\\\\Retail\\\\Software\\\\POSusb\\\\[\\x00-\\x7F]*"
    Should Match Regexp   ${string_1}  ${string_2}

Bad Scape P - damies13 Dave Solution

    ${string_1}=   Set Variable  "prefixesC:\\Program Files\\WNPOS\\postfixes"
    ${string_2}=   Set Variable  "[\\x00-\\x7F]*C:\\\\Program Files\\\\WNPOS\\\\[\\x00-\\x7F]*"
    Should Match Regexp   ${string_1}  ${string_2}

TEST RESULT:
image

1 Like