user3925023
user3925023

Reputation: 687

Roboframework - Regex syntax

I'd like to inlcude a string RegEx match in a Robot framework test. In THIS link there's the working RegEX

This is my robotframework scrip code:

*** Settings ***
Library           String
Library            Collections
Library           OperatingSystem
Library           DateTime

*** Test Cases ***
RegTest
    ${result}=    Set Variable     https://www.instagram.com/test.profile/
    ${lines} =  Get Lines Matching Regexp       ${result}       Reg\\"(?:https?:)?\/\/(?:www\.)?(?:instagram\.com|instagr\.am)\/(?P<username>[A-Za-z0-9_](?:(?:[A-Za-z0-9_]|(?:\.(?!\.))){0,28}(?:[A-Za-z0-9_]))?)"im
    Log     ${lines} 

When I execute it it produce this output: enter image description here

So basically no match even if the RegEx proven works. Tried to look at the RoboFramework docs HERE but I'm not able to figure it out what I'm doing wrong. Did I miss something in the syntax? Thx

###UPDATE Robotframework regex syntax (from documentation):

${lines} =  Get Lines Matching Regexp   ${result}   Reg\\w{3} example   

Upvotes: 0

Views: 3185

Answers (1)

Bryan Oakley
Bryan Oakley

Reputation: 386010

Robot's regular expression syntax is the same for python. The one exception is that you need to use two backslashes if your regular expression needs one since robot uses the backslash as its own substitution character before passing the expression to the keyword.

Here's how I would do the pattern:

*** Variables ***
${pattern}  SEPARATOR=
...  https?                          # http followed by optional "s"
...  ://                             # the literal string "://"
...  (www\\.)?                       # optional "www."
...  (instagram\\.com|instagr\\.am)  # either "instagram.com" or "instagr.am"
...  /                               # literal "/"
...  [a-zA-Z0-9_.]+                  # one or more alphanumeric plus "_" and "."

Note: I don't know what the official specs are. For example, do you require a user profile? Can the profile contain any characters? Must there be a trailing "/"? Regardless, you can start with this pattern and then tweak it to meet your requirements.


Here's a complete robot test:

*** Settings ***
Library           String

*** Variables ***
${pattern}  SEPARATOR=
...  https?                          # http followed by optional "s"
...  ://                             # the literal string "://"
...  (www\\.)?                       # optional "www."
...  (instagram\\.com|instagr\\.am)  # either "instagram.com" or "instagr.am"
...  /                               # literal "/"
...  [a-zA-Z0-9_.]+                  # one or more alphanumeric plus "_" and "."

*** Keywords ***
String should match pattern
    [Arguments]  ${url}
    ${matches}=  Get lines matching regexp  ${url}  ${pattern}
    run keyword if  not $matches
    ...  fail  '${url}' did not match

String should NOT match pattern
    [Arguments]  ${url}
    ${matches}=  Get lines matching regexp  ${url}  ${pattern}
    run keyword if  $matches
    ...  fail  '${url}' matched, but shouldn't have

*** Test Cases ***

URLs that should match
    [template]  String should match pattern

    http://www.instagram.com/test.profile
    https://www.instagram.com/test.profile
    http://instagram.com/test.profile
    http://www.instagram.com/test.profile
    http://www.instagr.am/test.profile
    https://www.instagr.am/test.profile
    http://instagr.am/test.profile
    http://www.instagr.am/test.profile

URLs that should NOT match
    [template]  String should NOT match pattern

    http://www.instagr.am                  # no user profile
    http://www.instagr.am/foo-bar          # profile with illegal character
    xttp://www.instagram.com/test.profile  # xttp instead of http
    http://www.instagr.com/test.profile    # instagr.com
    bogus                                  # everything wrong

Upvotes: 1

Related Questions