I have been trying to create a solid regular expression to match a
possible multi-line expression without success. So after several hours i
almost got there but not the point i would like, hoping somebody can
point me in the right direction.
Here is an example i am dealing with:
01xxxxxxxxxxxxxx
-:
<20>ABCD
<30>edfghi212
-|
-:
<20>EFGH
<30>hjkli3232
-|
89xxxxxxxxxxxxx
I need to match anything that is enclosed in between "|-:" and "-|"
So far i've got "/^\{\|-:$.*^-\|$/m" , this one is greedy, returning the
complete set, instead of each match, i just haven't figure out how to
make it reluctant enough to return one by one.
The returned matches expected must be something like this:
1:
-:
<20>ABCD
<30>edfghi212
-|
2:
-:
<20>EFGH
<30>hjkli3232
-|
Currently is returning:
1:
-:
<20>ABCD
<30>edfghi212
-|
-:
<20>EFGH
<30>hjkli3232
-|
Any suggestion is greatly appreciated.
And finally, any good regular expressions book ?? =)
On Fri, Nov 19, 2010 at 1:42 PM, Guillermo Riojas <guillermo.riojas@gmail.com> wrote:
Hi,
I have been trying to create a solid regular expression to match a
possible multi-line expression without success. So after several hours i
almost got there but not the point i would like, hoping somebody can
point me in the right direction.
Here is an example i am dealing with:
I need to match anything that is enclosed in between "|-:" and "-|"
So far i've got "/^\{\|-:$.*^-\|$/m" , this one is greedy, returning the
complete set, instead of each match, i just haven't figure out how to
make it reluctant enough to return one by one.
The returned matches expected must be something like this:
I need to match anything that is enclosed in between "|-:" and "-|"
So far i've got "/^\{\|-:$.*^-\|$/m" , this one is greedy, returning the
complete set, instead of each match, i just haven't figure out how to
make it reluctant enough to return one by one.
If you're using ruby 1.9 you can do us that, *? is the reluctant version of *:
/^\|-:$.*?^-\|$/m
Note that I removed the '\{' from your original pattern. It is not
needed in this case.
If you're on 1.8, one possibility is:
/^\|-:$(?m:.*?)(?=^-\|)^-\|$/
But there are other, probably more efficient, ways to do it as well.
And finally, any good regular expressions book ?? =)
Jeffrey Friedl's Mastering Regular Expressions is an excellent read
and covers regular expressions inside and out, literally. Here's the
amazon link:
If you're using ruby 1.9 you can do us that, *? is the reluctant version of *:
/^\|-:$.*?^-\|$/m
---8<---
If you're on 1.8, one possibility is:
/^\|-:$(?m:.*?)(?=^-\|)^-\|$/
I was under the impression that the reluctant versions of the four
quantifiers was only available under ruby 1.9, but they are apparently
available under 1.8 as well. I used it in the example I showed for 1.8
without noticing.
Regards,
Ammar
···
On Fri, Nov 19, 2010 at 3:50 PM, Ammar Ali <ammarabuali@gmail.com> wrote:
On Fri, Nov 19, 2010 at 3:50 PM, Ammar Ali <ammarabuali@gmail.com> > wrote:
If you're using ruby 1.9 you can do us that, *? is the reluctant version of *:
/^\|-:$.*?^-\|$/m
---8<---
If you're on 1.8, one possibility is:
/^\|-:$(?m:.*?)(?=^-\|)^-\|$/
I was under the impression that the reluctant versions of the four
quantifiers was only available under ruby 1.9, but they are apparently
available under 1.8 as well. I used it in the example I showed for 1.8
without noticing.
Regards,
Ammar
Thanks for clarification Ammar, it works perfectly, and also for the
book recommendation very useful , thanks a lot