Dhamo
Dhamo

Reputation: 1251

Karate XML assertion : Iterate to match a pattern inside an attribute

I have this regex pattern, (\-?\d?)(\.?\d)*\,{1}(\-?\d+\.?\d*) for the following data

<LinearRing>
    <coordinates> 46.8,16.600001 023,16.600035 -46.2000,16.60004 46,164 47.400044,-14.1 47.400043,13.8 47.400043,0 47.400024,-3.9  </coordinates>
    <coordinates> 46.8,16.600032 46,16.600035 -46.2000,16.60004 46,164 47.400044,-14.1 47.400043,13.8 47.400043,0 47.400024,-3.9  </coordinates>       
</LinearRing>

Due to space/gap (between 16.600001 and 023), the matching fails, I also tried adding \\s? but it is still failing,

      # * def xmlData1 = above data
        * match each $xmlData1/LinearRing/coordinates == '#regex ((\\-?\\d?)(\\.?\\d)*\\,{1}(\\-?\\d+\\.?\\d*)\\s?)'

Is this possible to iterate each group (of 2 values) inside the coordinates to match the above regex in Karate? Or how I can adjust my regex to match the format? only one space is allowed between each value

Upvotes: 2

Views: 104

Answers (1)

Peter Thomas
Peter Thomas

Reputation: 58058

You should think of other approaches:

* def input = ' 46.8,16.600001 023,16.600035 -46.2000,16.60004 46,164 47.400044,-14.1 47.400043,13.8 47.400043,0 47.400024,-3.9  '
* def temp = input.trim().split(' ')
* match each temp == '#regex .*'

Let me also say that in my opinion these assertions you are doing seem to be a waste of time. Are you really gaining anything by checking these regexes ? I suggest you work with your leads and product owner if applicable.

What I would do is write a Java class that can parse and "make sense" of these numbers - whether they are valid co-ordinates etc. But up to you.

Upvotes: 1

Related Questions