[Gambas-user] Regular expressions

Cristiano Guadagnino criguada at gmail.com
Thu Dec 30 14:54:23 CET 2021


Il giorno gio 30 dic 2021 alle ore 14:01 Hans Lehmann <hans at gambas-buch.de>
ha scritto:

> Hello.
>
> I am looking for 3 regular pattern expressions that check in a DokuWiki
> text whether the formats `underlined`, `italic` or `bold` are in the
> given text.
>
> Example text:
>
> The //installation// of a **SSH server** on the remote __computer__ is
> worthwhile in any case!
>
> My many attempts ended up with these patterns, which unfortunately do
> not work:
>
> IF sLine Like "*[_]{2}*[_]{2}*" THEN sLine = Replace(sLine, "__", "<uuu>")
> IF sLine Like "*[*]{2}*[*]{2}*" THEN sLine = Replace(sLine, "**", "<bbb>")
> IF sLine Like "*[/]{2}*[/]{2}*" THEN sLine = Replace(sLine, "//", "<iii>")
>
>
Hi Hans!
I see various problems in your regular expressions.

First of all, a regexp usually matches any part of a line of text, so you
do not have to add "*" to the start and end of the regexp (and that would
be wrong anyway, more later). If you want to match an entire line (but this
is not your case), you should use the start_of_line (^) and end_of_line ($)
characters.

Secondly the characters you want to match are mostly special characters, so
you have to escape them. E.g. the star (*) character is a quantifier in a
regexp, so if you want to match a literal star you have to escape it with a
backslash (\*).

I would write your first expression this way:
"\*\*.*\*\*"
or, if you want to capture what's between the double-star characters:
"\*\*(.*)\*\*"

That regexp I wrote means: try to match a text that begins with two stars,
then has any number of characters (but not zero) and then has again two
stars.
The central part of the regexp is a single dot (.) that represents any
character followed by the quantifier star (*) that means "any positive
number of the preceding character, but not zero".

I think you should study regular expressions some more, but this
introduction should already be enough for you to write the other two
expressions.

Hope this helps!
Cris
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.gambas-basic.org/pipermail/user/attachments/20211230/c7ec019f/attachment.htm>


More information about the User mailing list