Robot Framework - 正则表达式语法
Roboframework - Regex syntax
我想在 Robot 框架测试中包含字符串 RegEx 匹配。
在 THIS link 中有有效的 RegEX
这是我的 robotframework 代码:
*** 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
机器人的正则表达式语法与 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
我想在 Robot 框架测试中包含字符串 RegEx 匹配。 在 THIS link 中有有效的 RegEX
这是我的 robotframework 代码:
*** 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
机器人的正则表达式语法与 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