Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

robotframework - Replace content with regex

For robotframework Replace String Using Regexp, I need to write a regex but I'm newbie can you help me ?

Need to transform in a String each occurs like {{string1.string2.string3.string4}} to ${string1_string2_string3_string4}

So the transformations are :

{{ will be ${

}} will be }

.  will be _

For example I got a string (or a file..) that contain ;

{
"my.value_1{}":{{string1.string2.string3.string4}},
"my_value2.}{":{{string1.string2.string3.string4}},
"my_value3":{{string1.string2}}10.64
}

I want it to be :

{
"my.value_1{}":${string1_string2_string3_string4},
"my_value2.}{":${string1_string2_string3_string4},
"my_value3":${string1_string2}10.64
}

As you can see, only occurs that in format {{string1.string2.string3.string4}} or {{string1.string2}}... must to be transformed by the regex. Chars like { } . must not be transformed if not like format that I said.

Thanks a lot!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You example is not very clear, please be more concrete with your example next time.


I assume this is your initial string:

{{string1.string2.string3.string4}}

and this is a desired outcome:

${string1_string2_string3_string4}

You can achieve that in a few steps:

Substitute
    ${variable}=    Set Variable    {{string1.string2.string3.string4}}    
    ${variable}=    Replace String Using Regexp    ${variable}    [.]    _
    ${variable}=    Replace String Using Regexp    ${variable}    [{]{2}    {
    ${variable}=    Replace String Using Regexp    ${variable}    [}]{2}    }
    ${variable}=    Set Variable    $${variable}
    Log To Console    ${variable}

This will log to console this string:

${string1_string2_string3_string4}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...