【问题标题】:Roboframework - Regex syntax机器人框架 - 正则表达式语法
【发布时间】:2021-05-07 07:44:09
【问题描述】:

我想在 Robot 框架测试中包含一个字符串 RegEx 匹配。 在THIS 链接中有工作的正则表达式

这是我的机器人框架脚本代码:

*** 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} 

当我执行它时,它会产生以下输出:

所以即使 RegEx 证明有效,基本上也没有匹配项。 试图查看 RoboFramework 文档HERE,但我无法弄清楚我做错了什么。 我错过了语法中的某些内容吗? 谢谢

###更新 Robotframework 正则表达式语法(来自文档):

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

【问题讨论】:

  • 看起来应该是Reg "^(?:https?://)?(?:www\\.)?(?:instagr(?:\\.am|am\\.com))/([\\w.-]+)/$"
  • 您是否要匹配以五个字节 Reg\" 开头的字符串?
  • 没有 @BryanOakley 只是想符合和理解 RoboFramework 正则表达式语法,我还在上面的问题中更新了一个摘录
  • @WiktorStribiżew 尝试 y 语法不起作用
  • 好吧,在您的情况下,Reg 看起来像正则表达式的一部分。那么它应该是${lines} = Get Lines Matching Regexp ${result} ^(https?://)?(www\\.)?(instagr(\\.am|am\\.com))/([a-zA-Z0-9_.-]+)/$。让我们回到括号表达式和捕获组。为了使它不区分大小写和多行,你需要(?im)(?im)^(https?://)?(www\\.)?(instagr(\\.am|am\\.com))/([a-zA-Z0-9_.-]+)/$

标签: regex robotframework


【解决方案1】:

Robot 的正则表达式语法与 python 相同。一个例外是,如果您的正则表达式需要一个反斜杠,您需要使用两个反斜杠,因为机器人在将表达式传递给关键字之前使用反斜杠作为自己的替换字符。

这是我将如何做的模式:

*** 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 "."

注意:我不知道官方规格是什么。例如,您需要用户资料吗?配置文件可以包含任何字符吗?必须有一个尾随“/”吗?无论如何,您可以从这种模式开始,然后对其进行调整以满足您的要求。


这是一个完整的机器人测试:

*** 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

【讨论】:

    猜你喜欢
    • 2017-06-06
    • 2015-01-02
    • 2020-05-12
    • 2019-03-16
    • 1970-01-01
    • 2010-12-04
    • 1970-01-01
    • 1970-01-01
    • 2011-03-22
    相关资源
    最近更新 更多