[Gambas-user] Regexp Submatches
Rob
sourceforge-raindog2 at ...94...
Wed Jun 25 19:55:26 CEST 2008
On Wednesday 25 June 2008 10:21, Nathan Neff wrote:
> For example, I want to find all non-whitespace characters in the
> string "Gambas", and
> iterate through them. I tried code like this:
> rege.Compile("(\\S)")
> FOR i = 1 TO rege.SubMatches.count
> Message(rege.SubMatches[i].Text)
> But I can only get it to print out the "g" character.
Submatches refer to the parts of your regular expression in
parentheses; matches don't automatically repeat unless you call Exec
multiple times. For example, if you wrote
rege.Compile("(\\S)..(\\S).(\\S)")
you would get three entries in Submatches, which would be g, b and s.
You should theoretically be able to do this, but I just tested it in
a Perl one-liner and there it only returns the last character:
rege.Compile("(\\S)+")
A better solution is to use the Offset property and write your own
method to do a global search (here's some pseudocode to get you
started:)
private function GRX(pattern as string, subject as string) as String[]
dim re as new Regexp
dim matches as new String[]
dim tmp as string
tmp = subject
re.Compile(pattern)
re.Exec(tmp)
do while re.text <> ""
matches.add(re.text)
tmp = mid(tmp, re.offset + 2) ' may need to be 1, can't remember
re.Exec(tmp)
loop
return matches
end
I probably should have just come up with some way to do that inside
the component, but it didn't occur to me at the time.
I see that most of the class member documentation never made it over
from the old Wiki to the current one, so I'll try to take some time
and fix that if I can find my old static copy of the wiki. I wonder
if it would be useful to at least default to a syntax summary (as in
VB's class inspector) rather than prompting the user to edit the page
if the documentation's missing. As it is, if you edit the page but
leave it blank and save it, the wiki inserts the syntax summary
anyway.
Rob
More information about the User
mailing list