Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write a regular expression utilizing the Robot Framework to find/replace various date strings

I have an automated test using the Robot Framework that reads a file into a variable. I'm parsing that variable for various date formats and trying to replace the date with the current date using a regex.

What I am struggling with is getting a regex to work in the Robot Framework (I've written a regex in various websites like pythex and regex101 that appears to work with Python for what I need).

${date}=  get current date
${datetime}=  convert date    ${date}  datetime
${MonthList}=  create list  January  February  March  April  May  June  July  August  September  October  November  December
${monthName}=  get from list  ${MonthList}  ${datetime.month-1}    
${ExpectedFileAsString}=  set test variable  January 23, 2009 May 1, 2020 05/21/1990 05/1/1990 5/21/1990 5/2/1990

${ExpectedFileAsString}=  replace string using regexp  ${ExpectedFileAsString}  ([A-Z][a-z]+\s\d+,\s\d\d\d\d)  ${monthName} ${datetime.day}, ${datetime.year}

${match1}=  get regexp matches  ${ExpectedFileAsString}  [A-Z][a-z]+\s\d+,\s\d\d\d\d
log  matches 1: ${match1}  console=yes

${match2}=  get regexp matches  ${ExpectedFileAsString}  [A-Z][a-z]{2,8}\s\d{1,2},\s\d{4}
log  matches 2: ${match2}  console=yes

${ExpectedFileAsString}=  replace string using regexp  ${ExpectedFileAsString}  ([A-Z][a-z]{2,8}\s\d{1,2},\s\d{4})  ${monthName} ${datetime.day}, ${datetime.year}

${ExpectedFileAsString}=  replace string using regexp  ${ExpectedFileAsString}  (?<![/\d])\d{1,2}/\d{1,2}/\d{4}(?![/\d])  ${datetime.month}/${datetime.day}/${datetime.year}

When I use the regex's I provided in my examples against the provided string on the 2 regex websites, they appear to do everything I need. When I run them using the Robot Framework, they get no hits. I must be missing something?

like image 655
Chuntinator Avatar asked Jan 28 '19 21:01

Chuntinator


People also ask

How do you write regular expressions in Robot Framework?

Robot framework will strip one level of backslash before it is used as a regular expression. See the section titled Escaping in the robot framework user guide. Thus, if your expression has something like \s , it will appear to the pattern matcher as a plain s .

How do you write if else condition in robot framework?

END Use Run Keyword If in Robot Framework Run Keyword If ${True} Log This line IS executed. Run Keyword If ${False} Log This line is NOT executed. Use Run Keyword Unless in Robot Framework Run Keyword Unless ${True} Log This line is NOT executed. Run Keyword Unless ${False} Log This line IS executed.


1 Answers

Robot framework will strip one level of backslash before it is used as a regular expression. See the section titled Escaping in the robot framework user guide.

Thus, if your expression has something like \s, it will appear to the pattern matcher as a plain s. The solution is to escape the backslashes with another backslash (eg: \\s\\d+ instead of \s\d+).

Here's a short passing test that illustrates the point.

*** Variables ***
${Example String}  January 23, 2009

*** Test Cases ***
Example
    Run keyword and expect error
    ...  'January 23, 2009' does not match 'Januarys23'
    ...  Should match regexp  ${Example String}  January\s23

    Should match regexp  ${Example String}  January\\s23
like image 53
Bryan Oakley Avatar answered Sep 20 '22 10:09

Bryan Oakley