ZeroHero
ZeroHero

Reputation: 21

Get substring with Robot Framework

I have a string "banana :is_good OR :bad and apple :is_sweet OR :bitter and..."

I want to get all the characters between ":" characters.

(so in upper case I would get "is_good OR " and "is_sweet OR ")

What is the simplest way to do it with Robot Framework? Substring/Get Regexp Matches/?

Or if it is better to do with Python, how to do it?

Upvotes: 0

Views: 1055

Answers (2)

Bryan Oakley
Bryan Oakley

Reputation: 386325

You can use python's re module and the evaluate keyword:

*** Variables ***
${string}      banana :is_good OR :bad and apple :is_sweet OR :bitter and...
@{expected}    is_good OR${SPACE}   bad and apple${SPACE}  is_sweet OR${SPACE}

*** Test Cases ***
Example
    @{substrings}=  evaluate  re.findall(r'(?<=:)[^:]+(?=:)', $string)
    Should be equal  ${substrings}  ${expected}

Upvotes: 1

Jiri Janous
Jiri Janous

Reputation: 1242

As you described in your question there are a lot of possibilities. For example you can use Evaluate keyword and treat it as python problem. Below code should work:

***Variables***
${text}    banana :is_good OR :bad and apple :is_sweet OR :bitter and...
  
*** Test Cases ***   
Test
    ${searched_text}=    Evaluate    [x for i,x in enumerate("""${text}""".split(":")) if i%2 !=0 ]
    Log    ${searched_text}

Upvotes: 1

Related Questions