How to capture a substring from a string in robot-framework

3,266 views
Skip to first unread message

dineshbh...@gmail.com

unread,
Sep 2, 2020, 7:59:15 AM9/2/20
to robotframework-users
Hello,

i want to know on  how to capture sub-string from a given string based on regular expression grouping like in python.

I tried with "Get Lines Containing String" keyword. But, it displays the entire line
Tried with other keywords also. but, not successful:
#${line}    Get Lines Containing String    ${output}    H1
#${line}    Get Lines Matching Pattern    ${output}    H1

Basically, i want to capture the first substring "13fdc377-f509-4df0-bf87-df7285f228b0" if the line matches "H1" which  is there in the line ${output}. Please suggest on how to do it in robot framework. This can be  achievable in python with regular expression and grouping. Please suggest.

${output}    '| 13fdc377-f509-4df0-bf87-df7285f228b0 | -    | 8c8798ef-bb3b-4bf6-abad-8bdd63d730d0 | H1               |'
*** Test Cases ***
PrintRegex
    ${line}    Get Lines Containing String    ${output}    H1
    Log To Console    ${line}


p_kumar

unread,
Sep 2, 2020, 10:18:25 AM9/2/20
to robotframework-users
Hi Dinesh,

I don't know if your variables  ${output} and  ${line}  can contain multiple lines(Which could require further splitting  ${line}  into lines)  but here's what you can use for now.
Please find the below code:

${output}       '| 13fdc377-f509-4df0-bf87-df7285f228b0 | -  | 8c8798ef-bb3b-4bf6-abad-8bdd63d730d0 | H1  |'

*** Test Cases ***
PrintRegex
  ${line}  Get Lines Containing String  ${output}  H1
  Log  ${line}
  @{word_list}=  Split String  ${line}  |
  Log  @{word_list}[1]


Console Output
Starting test: first testProj.TestAutomationSuite1.PrintRegex
20200902 19:35:47.751 :  INFO : 1 out of 1 lines matched
20200902 19:35:47.751 :  INFO : ${line} = '| 13fdc377-f509-4df0-bf87-df7285f228b0 | - | 8c8798ef-bb3b-4bf6-abad-8bdd63d730d0 | H1 |'
20200902 19:35:47.753 :  INFO : '| 13fdc377-f509-4df0-bf87-df7285f228b0 | - | 8c8798ef-bb3b-4bf6-abad-8bdd63d730d0 | H1 |'
20200902 19:35:47.754 :  INFO : @{word_list} = [ ' |  13fdc377-f509-4df0-bf87-df7285f228b0  |  -  |  8c8798ef-bb3b-4bf6-abad-8bdd63d730d0  |  H1  | ' ]
20200902 19:35:47.755 :  INFO :  13fdc377-f509-4df0-bf87-df7285f228b0 
Ending test:   first testProj.TestAutomationSuite1.PrintRegex


Thanks

tothsz...@gmail.com

unread,
Sep 2, 2020, 10:44:02 AM9/2/20
to robotframework-users
Hej!

And if you really want to use regex, you can do it with Get Regexp Matches.

*** Variables ***
${OUTPUT}    | 13fdc377-f509-4df0-bf87-df7285f228b0 | -    | 8c8798ef-bb3b-4bf6-abad-8bdd63d730d0 | H1               |

*** Test Cases ***
Extract UUIDs From String #1
    ${uuids}=    Get Regexp Matches    ${OUTPUT}    [0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12}
    Log To Console    ${uuids}
    Log To Console    ${uuids[0]}

The output will be the following:

==============================================================================
Regex                                                                         
==============================================================================
Extract ID From String                                                .All UUIDs: ['13fdc377-f509-4df0-bf87-df7285f228b0', '8c8798ef-bb3b-4bf6-abad-8bdd63d730d0']
.First UUID: 13fdc377-f509-4df0-bf87-df7285f228b0



I also have an improvement idea for Kumar's solution.
First, remove the pipe lines and after that you can use split.
In this case you will get rid of the unnecessary empty strings in the list.

*** Test Cases ***
Extract ID From String #2
    ${output_without_spaces}=    Remove String    ${OUTPUT}    |
    @{words}=    Split String    ${output_without_spaces}
    Log To Console    Words: ${words}
    Log To Console    First UUID: ${words[0]}

The output will be the following:

==============================================================================
Regex                                                                         
==============================================================================
------------------------------------------------------------------------------
Extract ID From String #2                                             ..Words: ['13fdc377-f509-4df0-bf87-df7285f228b0', '-', '8c8798ef-bb3b-4bf6-abad-8bdd63d730d0', 'H1']
.First UUID: 13fdc377-f509-4df0-bf87-df7285f228b0

Reply all
Reply to author
Forward
0 new messages