机器人框架字符串格式

Robot Framework String Formatting

如何制作如下字符串格式:

${original} = MZ**77050000*******228  
${desired} =  MZXX 7705 0000 XXXX XXXX 28

我设法将 * 替换为 X,并添加如下空格,但仍无法将倒数第三个 int 格式化为 X:

${rep_temp} =  Replace String  ${original}    *   X

FOR  ${i}  IN  4  8  12  16  20  22
            ${y} =   Evaluate    ${i} - 4
            ${temp} =       Get Substring     ${rep_temp}    ${y}    ${i}
            ${new_temp} =  Set Variable      ${new_temp} ${temp}
            ${new_temp} =  Strip String      ${new_temp}
            log to console     new_temp-->${new_temp}
        END

我会使用字符串库中的 Replace String Using Regexp 关键字。我不知道原始字符串的确切标准,但假设有 ******* 所以 7 * 后跟 3 位数字并且您想将 3 的第一个数字转换为 * , 然后到 X.

你可以这样做:

*** Settings ***
Library     String

*** Variables ***
${original}    MZ**77050000*******228 

*** Test Cases ***
Test Replace
    ${desired}=    Replace String Using Regexp    ${original}    \*\{7\}\d     ********
    ${desired}=    Replace String Using Regexp    ${desired}     \*     X
    # add the spaces

您必须在机器人文件的正则表达式中使用双重转义,并且还必须转义 {},因为它们是 Robot Framework 的特殊字符。所以这个 \*\{7\}\d 等价于这个 \*{7}\d 并且将匹配 7 * 后跟一个数字。

这就是输出,您必须添加空格并完成。