1

I have data in the following format

name                      | height      | hex assigned
[alphanumeric and spaces] | [numeric] cm| [hex]:[hex]
20 char                   | 5 char      | 1 char: 1 char

What I'm trying to do is, to match the regexp

${escaped}=  Regexp Escape  '[A-Za-z0-9_\s] | [0-9]+ cm| [0-9a-f]:[0-9a-f]'
Should Match Regexp  ${text}  ${escaped}

Although the text is correct, the test keep failing.

Would really appreciate if someone could point out what I did wrong.

3
  • Please create a short, complete program that demonstrates the error. See stackoverflow.com/help/mcve for more info. Commented May 26, 2015 at 15:11
  • in your regex where did you specify 20 chars and 5 chars? Commented May 26, 2015 at 15:12
  • the | is a meaningful character in most regex syntaxes, so you might have to escape it. Commented May 26, 2015 at 15:12

2 Answers 2

3

If you escape your regular expression, you're essentially converting the expression into a fixed string. You also have the problem that your pattern begins and ends with a single quote. Since robot treats the whole cell as the expression, your expression will only match if it actually begins and ends with a single quote.

The solution requires a few changes in what you're doing:

  1. remove the single quotes from the expression, unless your actual data also has single quotes
  2. don't call Regexp Escape
  3. do escape the pipes in the pattern, since those are treated specially in regular expressions
  4. add anchors (^ and $) unless you want to match the pattern anywhere in the string
  5. Remember that \ is special in robot files, so to get a backslash in the pattern you must include two when defining the pattern

I think the following does what you want:

*** Variables ***
${pattern}    ^[A-Z0-9_\\s]{1,20} \\| [0-9]+ cm\\| [0-9A-Fa-f]:[0-9A-Fa-f]$

*** Test cases ***
Example
    Should Match Regexp        12345678901234567890 | 1 cm| 3:5    ${pattern}
Sign up to request clarification or add additional context in comments.

Comments

2

You can use the following:

^[A-Za-z0-9_\s]{1,20}\s+\|\s+[0-9]{5} cm\|\s+[0-9a-f]:[0-9a-f]$

See DEMO

9 Comments

regular expressions within robot framework can be tricky. I think you need to show how to use this in a robot framework test case or keyword for the answer to be useful.
Your answer doesn't work. For one, you've included single quotes in the pattern but the data doesn't have single quotes. For another, Regexp Escape escapes all of the regular expression special characters, essentially converting the pattern into a fixed string.
@BryanOakley I dont use robot framework.. regex works fine.. robot syntax I used from this doc
That's my point: while you've given a pure regular expression solution, it's not very useful because the OP will have to figure out how to use it in the context of a robot test case or keyword. Not only that, but you've made the answer worse by calling Regexp Escape without realizing what it does.
@BryanOakley you are right about Regexp Escape.. but when it is used with Should Match Regexp it will work as expected.. see robotframework.googlecode.com/hg/doc/libraries/…
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.