wxRegEx woes

Volker Bartheld dr_versaeg at freenet.de
Mon Sep 18 03:48:17 PDT 2006


Hi Mike!

First of all: Thanks a lot for the input!

>> I want to extract single-character commands with optional argument
>> from a string using regular expressions. Command arguments are
>> encapsulated in single quotes. So i. e. "\\1", "\\h'Foobar'" are
>> valid.
>>
>> However, the backslash can be "escaped" itself - by doubling it.
>> "\\\\HelpMe" shouldn't be recognized as a command. I came up with
>> a RegExString of wxT("([^\\\\]|^)(\\\\[?])('[^']*')?" );
>>
>> So I ended with grouping the last two subexpressions like
>> wxT("([^\\\\]|^)((\\\\[?])('[^']*')?)") with MatchCount==2 since
>> non-capturing parentheses (see
>> http://www.regular-expressions.info/brackets.html) as in
>> wxT("(?:[^\\\\]|^)((\\\\[?])('[^']*')?)") didn't work.

>The non-capturing parentheses work if you use the 'advanced' syntax. 
>e.g. create the wxRegEx with wxRE_ADVANCED flag:
>	wxRegEx RegEx(RegExString, wxRE_ADVANCED);

Ah! I didn't know that...

>I don't think it helps in this case though. Although the text matching 
>it doesn't get captured as an additional subexpression, it's still 
>included in the main match.

Currently, it runs rather well with my lame solution. The syntax I have
to parse has grown over quite some time, so it's not very constistent -
a helthy mixture between HTML metatags and C-ish notation. That was why
I decided to use regular expression because I don't need to update the
code when our design guys come up with another set of brilliant ideas
but just the regex strings.

>Will it have to work on longish texts?

Rather not. I think below one kB or even less.

>If it needs to work on anything 
>but very short texts then the loop needs to be coded carefully so that 
>it will run in linear time. Basically:
>- keep the original text constant and move a pointer along it. 
>- use the overload of Matches that takes a length e.g. 
>'while(RegEx.Matches(pStr, 0, len))', since wxStrlen needs to be 
>avoided inside the loop and otherwise Matches will do wxStrlen 
>internally.
>- grow NewString exponentially.

Hopefully, time will not become an issue with my solution. Right now,
there are lots of other bottlenecks in the code... ;-)



Happy hacking!



Volker
__
Mail replies to/an V B A R T H E L D at G M X dot D E






More information about the wx-users mailing list