[Gambas-user] Help with RegExp
Bruce Bruen
bbruen at ...2308...
Sat Sep 24 01:10:53 CEST 2011
On Sat, 2011-09-24 at 00:14 +0200, tobias wrote:
> hi,
>
> i have a problem with regular expressions: i'm not very good in using them.
> i want to determine the comment string in a gambas comment with gb.pcre
> and been sitting down for 3 hours now, totally frustrated...
> i thought, i have to search for the first ' apostrophe from the left
> which isn't between "". i have experimented with the ugliest things but
> nothing worked properly.
> i used these lines to test:
>
> Print "'" '"'"
> Print "text" 'comment
> and the line of RegExp.Compile(???) itself
>
> of course, they should give
> "'"
> comment
> (nothing)
>
> i got a solution that matched the first two ones correctly but no chance
> for the regexp itself in line 3 (i always got something after the
> apostrophe in the regexp)...
>
> i didn't get very far without getting wrong results, so i think there's
> no need to post my tries
> i would be very glad if someone more experienced could help me.
>
> regards,
> tobi
>
I'm not sure whether you are trying to learn regexp patterns or parse a
gambas sourcecode line, but if the second then this is how I do it.
' Gambas module file
Public Sub Main()
Dim sSourceCode As String ' The original source line
Dim sCode As String ' The part of the source line that is code
Dim sComment As String ' The part of the source line that is comment
Dim aComment As New String[]
sSourceCode = " While surname<>\"O'Reilly\" or surname<>\"O'Malley\" Or surname <> \"O'Reilly-O'Malley\" '' Loop around looking for the irishman called \"O'Reilly\", \"O'Malley\" or \"O'Reilly-O'Malley\""
Print "Original==========="
Print sSourceCode
aComment = Split(sSourceCode, "'", "\"", False, True)
Print "Parsed==========="
Print aComment.Join("\n")
sCode = Trim(aComment[0])
aComment.Delete(0)
sComment = Trim(aComment.Join("'"))
Print "Code part==========="
Print sCode
Print "Comment part==========="
Print sComment
End
regards
Bruce
More information about the User
mailing list