Minato Arisato
Minato Arisato

Reputation: 45

Robot Framework Regexp

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.

Upvotes: 1

Views: 15942

Answers (2)

karthik manchala
karthik manchala

Reputation: 13640

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

Upvotes: 2

Bryan Oakley
Bryan Oakley

Reputation: 386010

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}

Upvotes: 3

Related Questions