jeudi 18 février 2016

Assitance with regex

I'm having a problem with getting only the nth match of a pattern. Here is what I'm trying to do:

I have the following text and I'm trying to only get the third match:

this is line one
this could also be line one
this is the line I'm interested in because is the one   // Line of interest
just some random line
this is not the line I want, it's not the one

I am using the following regex to fine lines that have a start string of "this" and end string of "one". So my regex is:

(?:^this.*?one)

Using RegEx Tester v3.2.0.0, I'm able to get the lines that match. The lines are 1, 2, 3 and 5. Line 4 is ignore because it obviously doesn't match.

Now I'm only interested in getting line 3 in return and ignore the other matches. So I thought that by using the occurrence {nth} and appending that to the end of my regex I would get the 3rd match. It doesn't seem to work. I'm not sure how to do this and I'm hoping someone can provide assistance. Any help is appreciated it. Thank you!

Aucun commentaire:

Enregistrer un commentaire