[Gambas-user] Regexpr appears not to be working
John Harrold
budgieboy52 at ...1601...
Sun Nov 30 11:59:58 CET 2008
Hi all
I am experimenting with the PCRE class to understand more about regular
expressions and Gambas in general
I have created a form with two textboxes, one for text input the other for
the regular expression text to test against.
There are a number of labels for displaying results returned from the call
to Regexp.
Below is the commented code for the change event for the input
textbox:txtInput.
The issue I have is with the line marked ***.
By looking at the PCRE class documentation it seems that the line should
return a string.
*PCRE Documentation on .SubMatches*
*RegexpSubmatches (gb.pcre)*
* .RegexpSubmatches contains the submatches returned from a
Regexp<http://gambasdoc.org/help/comp/gb.pcre/regexp?v3>match. You
access it with the SubMatches
property <http://gambasdoc.org/help/def/property?v3> of the Regexp class. *
* Each member of a .RegexpSubmatches collection simply contains the text of
the corresponding submatch. For example, with a subject of "quick brown fox"
and a pattern of "brown (\S+)", SubMatches[0] would contain "fox". *
It does, however, complain with the following message:
*Type mismatch: Wanted Number, Date or String, got .regexprSubMatches
instead.
*
The message seems to me to suggest that an array of .RegexpSubMatches is
being returned instead.
I have tried to read this returned array into a variety of arrays (string
and Object) but it always returns the above message.
If I comment out the line I get no errors and the call to Regex returns
Regex.Text, Regex.Offset and Regex.SubMatches.Count correctly.
Am I understanding the way to use the Regexpr correctly or have I made a
fundamental mistake somewhere?
Could it be a bug?
Budgieboy
*Code for input textbox:*
*PUBLIC SUB txtInput_Change()
DIM Regex AS Regexp
IF bCleared = TRUE THEN 'Flag to prevent error if try to send empty
string to Regex
bCleared = FALSE
ELSE
IF txtInput.Text = "" THEN 'We have empty input textbox, usually from
delete/backspace
txtInput.BackColor = Color.RGB(255, 165, 204)
RETURN
ENDIF
Regex = NEW Regexp(txtInput.Text, txtRegex.Text) 'Call Regexpr
lblResult.Caption = Regex.Text
lblMatch.Caption = Regex.SubMatches.Count
lblOffset.Caption = Regex.Offset
'This line below causes error:
'Type mismatch: Wanted Number, Date or String, got .regexprSubMatches
instead
*** IF Regex.SubMatches > 0 THEN lblSubMatches.Caption =
Regex.SubMatches[0].Text
'Indicate success or failure by changing input textbox backcolor
IF Regex.Text = "" THEN
txtInput.BackColor = Color.RGB(255, 165, 204) 'Pink - failure
ELSE
txtInput.BackColor = Color.RGB(55, 255, 125) 'Green - success
ENDIF
bCleared = FALSE
ENDIF
END*
More information about the User
mailing list