Robotframework - 在python中工作的正则表达式不匹配

Posted

技术标签:

【中文标题】Robotframework - 在python中工作的正则表达式不匹配【英文标题】:Robotframework - regex working in python not matching 【发布时间】:2021-12-20 21:23:14 【问题描述】:

我有以下字符串:

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

我需要用正则表达式提取这部分:

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

所以我创建了这个正则表达式(在 Python 中完美运行)

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

我已尝试将其包含在 robotsfrawework 脚本中:

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

但它根本不起作用(没有给出任何匹配)。 我无法理解我做错了什么。 谢谢

【问题讨论】:

【参考方案1】:

使用Get Regexp Matches 关键字代替Get Lines Matching Regexp

Get Lines Matching Regexp 用于在多行字符串中查找特定行。

代码:

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

输出:

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

【讨论】:

以上是关于Robotframework - 在python中工作的正则表达式不匹配的主要内容,如果未能解决你的问题,请参考以下文章

怎样搭建python和robotframework环境

python-selenium-robotframework安装问题

RobotFramework自动化测试框架-使用Python编写自定义的RobotFramework Lib

Mac下配置Python3+Eclipse+Robotframework

怎么在windows下安装robotframework-ride 1.5.2.1

RobotFramework之Python调用RF关键字