Need help running "Should Match Regexp"

Issue: My output has a line which contains the following:
“Maximum timeout: 20, Current timeout: 15”

I’ve tried created variables called patterns with the message summarized in Regex like the following:
*** Variables ***
${pattern1} ^Maximum timeout: 20
${pattern2} ^Maximum timeout: d+\,

*** TEST Cases ***
Test: MUST support this test

${output} Read Until secondary:host_name
Log to console ${output}
#Should Contain ${output} ${fwSessTOsRegex}
Should Match Regexp ${pattern1} ${pattern2}

Both attempts have failed and I’m not finding good RF documentation which clearly (to me) lays out how I should use the should match regexp operator. When the test fails, it indicates the following:

“Test:MUST support this test… | FAIL |
‘^Maximum timeout: 20’ does not match ‘^Maximum timeout: d+,’”

For the life of me, I can’t figure out how to order the robot framework syntax to properly match for a variable max time out. The value could be 60, 46, 25 or some other single double digit number for Maxiumum timeout and Current timeout. Please help.

${pattern2} ^Maximum timeout: d+,

This should be: ${pattern2}= ^Maximum timeout: \\d+,.*
Note: \ must always be escaped with another \ (in Python and RF is based in Python :wink: )

Did you tried in https://regex101.com/ ?
image

1 Like

Hi Scirh,

In addition to what @HelioGuilherme66 said, this is the documentation for Should Match Regexp (in case you hadn’t found it), in it’s examples it shows the escaped slash (\\) Helio mentioned, as well it has a link to more info on using Regular expressions in Robot Framework

I’ll second Helio’s recommendation to use regex101 for testing your regex patterns, it’s been very useful for me over the years.

Dave.

Thank you damies13 and Helio. The problem I am having isn’t with the actual regex, it is the basic Robot syntax of how to correctly and effectively use the ‘should match regexp’ built in.

*** Variables ***
${pattern2} ^Maximum timeout: \d+,.*

*** TEST Cases ***
Test Case: MUST support this test
${result1} Run some show command on a device
${result1} Should Match Regexp Maximum timeout: \d+, ignore_case=True

The test results in the following:
Test: MUST support standard stateful firewalling supporting sessio… …/bin/sh: show: command not found
Test: MUST support standard stateful firewalling supporting sessio… | FAIL |
ignore_case=True: ‘Maximum timeout:’ does not match ‘\d+,’

I’m ok with the regex but I’m using Robot Framework syntax incorrectly. That’s what I need help understanding how to do properly.

Update: I copied and pasted incorrect code to this chat. Updated now.

Thank you damies13 and Helio. The problem I am having isn’t with the actual regex, it is the basic Robot syntax of how to correctly and effectively use the ‘should match regexp’ built in.

*** Variables ***
${pattern2} ^Maximum timeout: \d+,.*

*** TEST Cases ***
Test Case: MUST support this test
${result1} Run some show command on a device
${result1} Should Match Regexp Maximum timeout: \d+, ignore_case=True

The test results in the following:
Test: MUST support standard stateful firewalling supporting sessio… …/bin/sh: show: command not found
Test: MUST support standard stateful firewalling supporting sessio… | FAIL |
ignore_case=True: ‘Maximum timeout:’ does not match ‘\d+,’

I’m ok with the regex but I’m using Robot Framework syntax incorrectly. That’s what I need help understanding how to do properly.

@TTnRobo You did not saw the documentation, as @damies13 mentioned.

Anyway, any good IDE would have the documentation for the keywords at hand.

Here is some examples of my pet project, RIDE:

And this is the code (also recommend for you to learn how to format here in the forum):

${result1}=    Should Match Regexp    ${output}    ${pattern2}    ignore_case=True

RIDE

Thank you again @Helio and @damies13 for taking the time to participate in this thread. With your help, I was able to determine how to properly string together the Robot Framework language/syntax so that my Regex is working as I expect. I’ve anonymized what I’m doing below but it is working. Problem solved. I do think it might be a bit redundant but that’s what I’ve got for now.

IMPORTANT QUESTION: Is the coloration thing you mentioned part of vs code or just that project RIDE you referred to?

*** Variables ***
${pattern1} Maximum timeout: \d+\, Current timeout: \d+.*
${pattern2} Maximum timeout: \d+\, Current timeout: \d+.*

*** TEST Cases ***
Test: MUST support this test
[. . . ]
${output} Read Until Regexp ${pattern1}
Should Match Regexp ${output} ${pattern2}
Log to console ${output}
Close SSH

Probably. You have to search, because I am not user of VSCode :wink: . There is the LSP project with a plugin for VSCode.