Search code examples
regexrobotframework

Robotframework - regex working in python not matching


I have the following string:

https://m.facebook.com/story.php?story_fbid=123345&id=12334&__xts__%5B0%5D=12.AboYsxZUMIF4f8fZJLZ0LVrJZJ1jxGxzsmM2lJZJ8JoGFxO1UgViHWKvUSGwxVjkzwOjfCxL-FxJY9pfgvYP6L8Kwdd4tHZl-V1OaVvl40agK3dimgois_14R0sr9rSelJ5rt0WTU5fRoHNeGv2TR3sxrewYBf2wzfMQwDKV4pV4WbLkbqFd0f1mbcpx7VjMduLhbj2MXRHO_ERg1ssEesWbCHAzvZNSmXP6LycMlYHhbaoi6m7gTpBswQR-VBkdfjk3289dfsfsdfoIgfSxaxr9oa6FATiaF2V54-QWQOKgYtuO87YzKFnNh-m_mGgVkCYIA7-YwEK10zZCmSxEtSrmWSl5ZzSXNRpMg&__tn__=%2As%2As

I need to extract with a regex this part:

https://m.facebook.com/story.php?story_fbid=123345&id=12334

So I created this Regex (works perfectly in Python)

https?://(www\.|m\.)(facebook\.com)\/story\.php\?story_fbid=[0-9]+&id=[0-9]+

I've tryed to include in a robotfrawework script:

*** Settings ***
Library           String

*** Variables ***
${pattern}  SEPARATOR=
...  https?                          
...  ://                            
...  (www\\.|m\\.)                   
...  (facebook\\.com)                
...  /story\\.php\\?story_fbid=                         
...  [0-9]+                     
...  &id=
...  [0-9]+


*** Test Cases ***
String should match pattern
    ${url}=     Set Variable    https://m.facebook.com/story.php?story_fbid=123345&id=12334&__xts__%5B0%5D=12.AboYsxZUMIF4f8fZJLZ0LVrJZJ1jxGxzsmM2lJZJ8JoGFxO1UgViHWKvUSGwxVjkzwOjfCxL-FxJY9pfgvYP6L8Kwdd4tHZl-V1OaVvl40agK3dimgois_14R0sr9rSelJ5rt0WTU5fRoHNeGv2TR3sxrewYBf2wzfMQwDKV4pV4WbLkbqFd0f1mbcpx7VjMduLhbj2MXRHO_ERg1ssEesWbCHAzvZNSmXP6LycMlYHhbaoi6m7gTpBswQR-VBkdfjk3289dfsfsdfoIgfSxaxr9oa6FATiaF2V54-QWQOKgYtuO87YzKFnNh-m_mGgVkCYIA7-YwEK10zZCmSxEtSrmWSl5ZzSXNRpMg&__tn__=%2As%2As
    ${matchesp}=  Get lines matching regexp  ${url}  ${pattern} 
    run keyword if  $matchesp   Log To Console  OK MATCHED!

But it simply don't work at all (not giving any match). I unable to understand what I'm doing wrong. Thx


Solution

  • Use Get Regexp Matches keyword instead of Get Lines Matching Regexp.

    Get Lines Matching Regexp is used to find the specific line in a multiline string.

    Code:

    *** Settings ***
    Library           String
    
    *** Variables ***
    ${pattern}        SEPARATOR=
    ...               https?
    ...               ://
    ...               (www\\.|m\\.)
    ...               (facebook\\.com)
    ...               /story\\.php\\?story_fbid=
    ...               [0-9]+
    ...               &id=
    ...               [0-9]+
    
    *** Test Cases ***
    String should match pattern
        ${url}=    Set Variable    https://m.facebook.com/story.php?story_fbid=123345&id=12334&__xts__%5B0%5D=12.AboYsxZUMIF4f8fZJLZ0LVrJZJ1jxGxzsmM2lJZJ8JoGFxO1UgViHWKvUSGwxVjkzwOjfCxL-FxJY9pfgvYP6L8Kwdd4tHZl-V1OaVvl40agK3dimgois_14R0sr9rSelJ5rt0WTU5fRoHNeGv2TR3sxrewYBf2wzfMQwDKV4pV4WbLkbqFd0f1mbcpx7VjMduLhbj2MXRHO_ERg1ssEesWbCHAzvZNSmXP6LycMlYHhbaoi6m7gTpBswQR-VBkdfjk3289dfsfsdfoIgfSxaxr9oa6FATiaF2V54-QWQOKgYtuO87YzKFnNh-m_mGgVkCYIA7-YwEK10zZCmSxEtSrmWSl5ZzSXNRpMg&__tn__=%2As%2As
        ${matchesp}=    Get Regexp Matches    ${url}    ${pattern}
        Log To Console    \n${matchesp}
    

    Output:

    ==============================================================================
    String should match pattern                                           ..
    ['https://m.facebook.com/story.php?story_fbid=123345&id=12334']
    String should match pattern                                           | PASS |
    ------------------------------------------------------------------------------
    Main                                                                  | PASS |
    1 test, 1 passed, 0 failed
    ==============================================================================