how to highlight regex matches in StyledTextCtrl widget?

If I want to highlight the range of characters that match a regular
expression in a StyledTextCtrl, how do I go about doing that? If I
call the FindText method it only returns the start of the matching
range. How do I know at what position the match ended? About the only
thing I can think of is to pull the text out and use the re module to
re-search to get the length of the matched data.

My problem is compounded by the fact my widget contains unicode data,
so one byte in the raw text doesn't necessarily correspond to one
position in the widget.

I'm using wxPython 2.8.9.1 and python 2.5

Hi,

If I want to highlight the range of characters that match a regular
expression in a StyledTextCtrl, how do I go about doing that? If I
call the FindText method it only returns the start of the matching
range. How do I know at what position the match ended? About the only
thing I can think of is to pull the text out and use the re module to
re-search to get the length of the matched data.

First I will say you will be much better off using the re module over
the builtin search functionality in the stc. The stc only implements a
fairly limited regular expression support.

But you can instead use the Search functions of the stc and they will
automatically update the text selection for you.

mystc.SearchAnchor()
mystc.SearchNext(wx.stc.STC_FIND_REGEXP, "Foo[a-z]* ")

My problem is compounded by the fact my widget contains unicode data,
so one byte in the raw text doesn't necessarily correspond to one
position in the widget.

You just need to calculate positions based on utf-8 character bytes as
that is what the stc uses internally.

Cody

···

On Tue, Mar 2, 2010 at 3:28 PM, Bryan Oakley <bryan.oakley@gmail.com> wrote:

Thank you. That is the information that I needed.

···

On Tue, Mar 2, 2010 at 3:47 PM, Cody Precord <codyprecord@gmail.com> wrote:

...
But you can instead use the Search functions of the stc and they will
automatically update the text selection for you.

mystc.SearchAnchor()
mystc.SearchNext(wx.stc.STC_FIND_REGEXP, "Foo[a-z]* ")

My problem is compounded by the fact my widget contains unicode data,
so one byte in the raw text doesn't necessarily correspond to one
position in the widget.

You just need to calculate positions based on utf-8 character bytes as
that is what the stc uses internally.