1

Just as a disclaimer, I just want to do this to make my life easier when reading logs.. Sometimes they have more than 100mb of text

I want to match a XML Group in which it contains some data.

Suppose I have a XML like below (and they are in the same line):

<car><id>1</id><acquiredDate>23-09-2016</acquiredDate><model>BMW</model></car>
<car><id>2</id><acquiredDate>23-09-2016</acquiredDate><model>BMW</model></car>
<car><id>3</id><acquiredDate>24-09-2016</acquiredDate><model>BMW</model></car>
<car><id>4</id><acquiredDate>23-09-2016</acquiredDate><model>BMW</model></car>

I want to match all cars that have were acquired on 23-09-2016. (3 matches on this case)

What I have so far is <car>.*?<acquiredDate>23-09-2016<\/acquiredDate>.*?<\/car>, but it will match the third and fourth car together. Something like:

<car><id>1</id><acquiredDate>23-09-2016</acquiredDate><model>BMW</model></car>

<car><id>2</id><acquiredDate>23-09-2016</acquiredDate><model>BMW</model></car>

<car><id>3</id><acquiredDate>24-09-2016</acquiredDate><model>BMW</model></car><car><id>4</id><acquiredDate>23-09-2016</acquiredDate><model>BMW</model></car>

I tried using something like <car>(?!.*<car>.*).*?<acquiredDate>23-09-2016<\/acquiredDate>.*?<\/car> but it will match only the last.

How I achieve that?

Felipe S.
  • 1,473
  • 2
  • 14
  • 22

1 Answers1

1

If you really want to go down the regex-matching-html route, then assuming you want to match the whole line, something like this would work:

/(?:^\<car\>[<\w>\/]+acquiredDate\>)(23\-09\-2016)(?:.+$)/gm
                                     ^^  ^^  ^^^^ 
                                 (change as required)
SierraOscar
  • 17,307
  • 5
  • 38
  • 67
  • This is exactly the match I needed! Sorry for being insistent in the regex thing.. It's because I'll use to find some text in a giant log. Now I can find my bugs !! thanks! – Felipe S. Sep 23 '16 at 22:36
  • 1
    @FelipeS. just to be clear, I'm not preaching about _not using regex_ (notice I didn't downvote your question - it's a perfectly good and valid question to ask) it's just that using a parser can make life easier. Ultimately whatever works for you is the right way so it's your preference. – SierraOscar Sep 23 '16 at 22:41