Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2011-08

Regex help needed please

9 messages · 4 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Graeme Geldenhuys

Hi,

I'm fairly new to regex. I know that the * is a greedy match. This is currently giving me undesired results. How do I rewrite the regex below so it will match up to the first } symbol it encounters.

To put this in context, I'm writing a regex based syntax highlighter. Here is sample code I am having trouble with:

   function MyFunc: {$ifdef FPC}longword{$else}word{$endif};

The following regex matches the first { symbol up until the last } symbol. :-( I obviously want it to stop at the first } it encounters.

This is the regex I'm currently using:

\s*\{\$.*\}

Any regex wizards that could help me here?

Regards,
- Graeme -

fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal http://fpgui.sourceforge.net/

Reply by email

ik
to Graeme Geldenhuys
Graeme Geldenhuys wrote 19 lines
Hi, I'm fairly new to regex. I know that the * is a greedy match. This is currently giving me undesired results. How do I rewrite the regex below so it will match up to the first } symbol it encounters. To put this in context, I'm writing a regex based syntax highlighter. Here is sample code I am having trouble with: function MyFunc: {$ifdef FPC}longword{$else}word{$endif}; The following regex matches the first { symbol up until the last } symbol. :-( I obviously want it to stop at the first } it encounters. This is the regex I'm currently using: \s*\{\$.*\}

Try this:
\{.*[^\}]\}

Show quoted text 18 lines
Any regex wizards that could help me here? Regards, - Graeme - -- fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal http://fpgui.sourceforge.net/ -- _______________________________________________ Lazarus mailing list http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20110810/de5c3599/attachment.html>

Reply by email

Graeme Geldenhuys
to ik
ik wrote
Try this: \{.*[^\}]\}

thanks for the quick response, but that doesn't seem to work either. You can try with Lazarus IDE's Find dialog. Just tick the "Regular expression" checkbox, and untick everything else. My code uses the same regex engine as Lazarus IDE does.

You left out the $ symbol after the first { symbol, but even so the following still matches too much.

\{\$.*[^\}]\}

I also tried this...

\{\$.*[^\{]\}

still no luck.

Regards,
- Graeme -

fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal http://fpgui.sourceforge.net/

Reply by email

ik
to Graeme Geldenhuys
Graeme Geldenhuys wrote 14 lines
On 08/10/2011 05:21 PM, ik wrote: > > Try this: > \{.*[^\}]\} thanks for the quick response, but that doesn't seem to work either. You can try with Lazarus IDE's Find dialog. Just tick the "Regular expression" checkbox, and untick everything else. My code uses the same regex engine as Lazarus IDE does. You left out the $ symbol after the first { symbol, but even so the following still matches too much.

can you please explain again what you are looking for ? the content inside the curly braces after the $ sign ?

Show quoted text 25 lines
\{\$.*[^\}]\} I also tried this... \{\$.*[^\{]\} still no luck. Regards, - Graeme - -- fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal http://fpgui.sourceforge.net/ -- _______________________________________________ Lazarus mailing list http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20110810/1940d518/attachment.html>

Reply by email

Graeme Geldenhuys
to Graeme Geldenhuys: I'm fairly new to regex. I know that the * is a greedy match. This is currently giving me…
Graeme Geldenhuys wrote 7 lines
The following regex matches the first { symbol up until the last } symbol. :-( I obviously want it to stop at the first } it encounters. This is the regex I'm currently using: \s*\{\$.*\}

Ah, I found it. TRegExpr supports the non-greedy syntax, so I changed the above regex to read:

\{\$.*?\}

*? makes it non-greedy. :-)

Regards,
- Graeme -

fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal http://fpgui.sourceforge.net/

Reply by email

ik
to Graeme Geldenhuys
Graeme Geldenhuys wrote 18 lines
On 08/10/2011 05:18 PM, Graeme Geldenhuys wrote: > > The following regex matches the first { symbol up until the last } > symbol. :-( I obviously want it to stop at the first } it encounters. > > This is the regex I'm currently using: > > \s*\{\$.*\} Ah, I found it. TRegExpr supports the non-greedy syntax, so I changed the above regex to read: \{\$.*?\} *? makes it non-greedy. :-)

Only on Perl like syntax, not on POSIX regex.

Show quoted text 17 lines
Regards, - Graeme - -- fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal http://fpgui.sourceforge.net/ -- _______________________________________________ Lazarus mailing list http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20110810/95977d85/attachment.html>

Reply by email

Martin
to Graeme Geldenhuys: thanks for the quick response, but that doesn't seem to work either. You can try with…
Graeme Geldenhuys wrote
On 08/10/2011 05:21 PM, ik wrote: > Try this: > \{.*[^\}]\} thanks for the quick response, but that doesn't seem to work either.

...

Show quoted text 9 lines
You left out the $ symbol after the first { symbol, but even so the following still matches too much. \{\$.*[^\}]\} I also tried this... \{\$.*[^\{]\}

without testing, from memory

All the above have

.* => any
then try to match ONE not "}"

So either use ( omit spaces, they are for readability)

\{ \$ .*? \}

or
\{ \$ [^ \} ]* \}

Reply by email