Forum Discussion

richie's avatar
richie
Community Hero
6 years ago

Check repeating .json attribute has either of two specific regex patterns

Hi!   A while back I needed some help to assert that a certain repeating attribute in a multi record json response had a specific regex pattern of 3 upper case chars.   Rao sorted me out - se...
  • nmrao's avatar
    nmrao
    6 years ago

    richie 

    Oh, data with valid and invalid could have helped to write the assertions.

     

    Anyways, seems you have trivial issue in the code snippet

     

    The following should work if your regex are correct

    assert json.value.code.every{it ==~ /[A-Z]{2}/ || it ==~ /[A-Z]{2}-[A-Z]{3}/}
  • nmrao's avatar
    nmrao
    6 years ago

    richie 

    The regex further can be simplified as (not tested though):

    ([A-Z]{2}(-[A-Z]{3})?)

     

    assert json.value.code.every{it ==~ /([A-Z]{2}(-[A-Z]{3})?)/}