www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Regex start/end position of match?

reply Gerald <me me.com> writes:
I'm using the std.regex API as part of Linux GUI grep utility I'm 
trying to create. I've got the GUI going fine using gtkd, the 
code to iterate over files (wow that was succinct in D, very 
impressive!), and getting matches via regex using the matchAll 
function.

I'm stuck though on how to get the start/end index of a match? 
Looking at RegexMatch, I don't see an obvious way to get this? 
I'll admit that coming from Java I'm not very comfortable with 
the Range concept in D, however I read the Range and Regex D 
documentation as well as Andrei's article on ranges referenced in 
the docs and I'm still not seeing what I'm missing.

By comparison, the Java API includes the start/end match index in 
the MatchResult interface. I have a feeling I'm not grokking 
something fundamental about Ranges in D, what am I missing?

BTW in case anyone asks, the reason I'm looking for this is to 
highlight the matches when displaying matching lines in the GUI.
Sep 30 2015
parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Thursday, 1 October 2015 at 03:29:29 UTC, Gerald wrote:
 I'm stuck though on how to get the start/end index of a match?
I couldn't find one either so I did the pre/post/hit things broken up. Take a look at this little program I wrote: http://arsdnet.net/dcode/replacer/ All the files it needs are there, so you can browse by http or download them somewhere and compile+run to give it a try. Lines 73-78 show the pre/hit/post thing, with me changing the colors of the output so they match. Afterward, I replace one instance and draw it again to show what the new line would look like. (The point of my program is to interactively request confirmation of every individual change you are going to make.) Something similar will probably work for you too.
Oct 01 2015
parent Gerald <me me.com> writes:
Thanks Adam, that was the hint I needed. For a given RegexMatch 
the pre().length() is essentially equivalent to the start 
position and taking pre().length + hit.length() gives the end 
position so I think this should be OK for my needs.
Oct 01 2015