Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I select a substring using a regexp in robot framework

In the Robot Framework library called String, there are several keywords that allow us to use a regexp to manipulate a string, but these manipulations don't seem to include selecting a substring from a string.

To clarify, what I intend is to have a price, i.e. € 1234,00 from which I would like to select only the 4 primary digits, meaning I am left with 1234 (which I will convert to an int for use in validation calculations). I have a regexp which will allow me to do that, which is as follows:

(\d+)[\.\,]

If I use Remove String Using Regexp with this regexp I will be left with exactly what I tried to remove. If I use Get Lines Matching Regexp, I will get the entire line rather than just the result I wanted, and if I use Get Regexp Matches I will get the right result except it will be in a list, which I will then have to manipulate again so that doesn't seem optimal.

Did I simply miss the keyword that will allow me to do this or am I forced to write my own custom keyword that will let me do this? I am slightly amazed that this functionality doesn't seem to be available, as this is the first use case I would think of when I think of using a regexp with a string...

like image 314
Cronax Avatar asked Oct 30 '15 10:10

Cronax


People also ask

How do you split text in robot framework?

Splits the string using separator as a delimiter string. If a separator is not given, any whitespace string is a separator. In that case also possible consecutive whitespace as well as leading and trailing whitespace is ignored. Split words are returned as a list.

How do you assign a string to a variable in Robot Framework?

Assigning variables# ${string} = Hello, world! @{list} Create List a b c # @{list} = [ a | b | c ] &{dict} Create Dictionary key1=val1 key2=val2 # &{dict} = { key1=val1 | key2=val2 } ${a} ${b} ${c} Create List a b c # ${a} = a, ${b} = b, ${c} = c ${cat_says} What Does The Cat Say # ${cat_says} = Meow!

Should robot framework contain string?

String is Robot Framework's standard library for manipulating strings (e.g. Replace String Using Regexp, Split To Lines) and verifying their contents (e.g. Should Be String). Following keywords from BuiltIn library can also be used with strings: Catenate. Get Length.


1 Answers

You can use the Evaluate keyword to run some python code.

For example:

| Using 'Evaluate' to find a pattern in a string
| | ${string}= | set variable | € 1234,00
| | ${result}= | evaluate | re.search(r'\\d+', '''${string}''').group(0) | re
| | should be equal as strings | ${result} | 1234

Starting with robot framework 2.9 there is a keyword named Get regexp matches, which returns a list of all matches.

For example:

| Using 'Get regexp matches' to find a pattern in a string
| | ${string}= | set variable | € 1234,00
| | ${matches}= | get regexp matches | ${string} | \\d+
| | should be equal as strings | ${matches[0]} | 1234
like image 194
Bryan Oakley Avatar answered Sep 19 '22 02:09

Bryan Oakley