【问题标题】:Regular Expression Robot Framework正则表达式机器人框架
【发布时间】:2017-06-06 14:20:04
【问题描述】:

我在 Robot Framework 中使用了使用 Regexp 函数替换字符串来替换“\|”到“###PIPE###”,但它不起作用。

*** Test Cases ***
TestCast1
${result}   Replace String Using Regexp   Bell|fieldName|11|LastName\|dd    \\\|    \#\#\#PIPE\#\#\#

结果

Bell###PIPIE###fieldName###PIPIE###11###PIPIE###LastName###PIPE###dd

我的预期结果

Bell|fieldName|11|LastName###PIPE###dd

你能帮帮我吗?

【问题讨论】:

  • 你能发布一个正确的输入字符串吗?
  • 输入字符串 = Bell|fieldName|11|LastName\|dd 不是吗?
  • 你的正则表达式函数是什么?
  • 不要使用正则表达式替换固定字符串。使用普通的Replace String 替换固定字符串。

标签: python regex automation automated-tests robotframework


【解决方案1】:

使用re.sub()函数的解决方案(如果输入字符串的最后一个“piped”部分可能是动态的):

input_str = 'Bell|fieldName|11|LastName\|dd'
result = re.sub(r'\\\|(\w+)$', r'###PIPE###\1', input_str)

print(result)

输出:

Bell|fieldName|11|LastName###PIPE###dd

【讨论】:

    【解决方案2】:

    让我们尝试一个粗略的想法,它很丑但很有效.. :)

    ${result}   Replace String Using Regexp   Bell|fieldName|11|LastName\|dd    \\|dd    \#\#\#PIPE\#\#\#dd
    Log To Console  \n\n${result}
    

    【讨论】:

      【解决方案3】:

      在为 Robot Framework 开发可嵌套的 For Loop 时,我遇到了类似的问题。这是我对正在发生的事情的理论,实际上没有您的关键字代码:

      事件 1:您将输入 Bell|fieldName|11|LastName\|dd\\\|\#\#\#PIPE\#\#\# 发送到 Replace String Using Regexp。这就是你输入的内容。

      事件 2:Replace String Using Regexp 接收 Bell|fieldName|11|LastName|dd\|###PIPE###。这会自动发生。

      事件 3:我假设您的关键字中的某些代码将 \| 放入另一个执行实际替换的关键字中。发生这种情况时,\| 将变为 |。然后,当该关键字替换文本时,它会找到四个 | 实例并将它们全部替换为 ###PIPE###

      修复

      事件 1:将输入 Bell|fieldName|11|LastName\\|dd\\\\|\#\#\#PIPE\#\#\# 发送到 Replace String Using Regexp

      事件 2:Replace String Using Regexp 接收 Bell|fieldName|11|LastName\|dd\\|###PIPE###

      事件 3:\\| 变为 \|。然后,当另一个关键字替换文本时,它会找到\| 的一个实例并将其替换为###PIPE###

      根据我在您的代码中看到的内容,| 不需要文字,因此唯一需要的是\s 和#s。所以,你发的和Bell|fieldName|11|LastName|dd|\#\#\#PIPE\#\#\#基本一样

      【讨论】:

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