Sign in Join

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

Mailing lists / lazarus / 2010-06

Regexp syntax

11 messages · 8 people · –

Reply by email Subscribe to this list Your subscription

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

Mattias Gaertner
to Juha Manninen: Hi any regexp gurus out there? For converter I need a regexp that matches a string…
Show quoted text 7 lines
Hi any regexp gurus out there? For converter I need a regexp that matches a string starting with "Tnt", not followed by "LX" and captures everything after "Tnt". This actually works: ^Tnt([^L][^X].+)

This would miss TIntLa and TIntaX

but I am sure it could be written in a cleaner way.

^Tnt(([^L]|.[^X]).+)

Show quoted text 5 lines
Then there is another regexp that captures string after "TntLX": ^TntLX(.+) and that is no problem.

Mattias

Reply by email

Juha Manninen

Hi

any regexp gurus out there?
For converter I need a regexp that matches a string starting with "Tnt", not followed by "LX" and captures everything after "Tnt". This actually works:

^Tnt([^L][^X].+)

but I am sure it could be written in a cleaner way.

Then there is another regexp that captures string after "TntLX":

^TntLX(.+)

and that is no problem.

Juha

Reply by email

Mattias Gaertner
to Juha Manninen
Show quoted text 6 lines
> > but I am sure it could be written in a cleaner way. > > ^Tnt(([^L]|.[^X]).+) Hmmm... It doesn't look very clean. It may be more correct but I am not sure of that.

It is not very clean. It does not match TntL and TntL1.

Does [^L]|.[^X] mean: anything but 'L' or anything, followed by anything but 'X' ?

The pipe is like an 'or' operator. [^L]|.[^X] means: Any character but L OR any character followed by any character but X. It matches a, b, La, Lb, but not LX and not L.

Even better regexp is:

^Tnt(([^L]|L[^X]).*|L$)

Mattias

Reply by email

waldo kitty
to Graeme Geldenhuys
Graeme Geldenhuys wrote
>> ^Tnt(([^L]|.[^X]).+) > Hmmm... It doesn't look very clean. I don't think *any* regex looks clean. ;-)

that because you aren't using the sandpaper on your eyeballs before studying the regex :P :lol:

Reply by email

Alexander Klenin
to Mattias Gaertner: On Tue, 15 Jun 2010 15:21:52 +0300 It is not very clean. It does not match TntL and TntL1.
Mattias Gaertner wrote 23 lines
On Tue, 15 Jun 2010 15:21:52 +0300 Juha Manninen wrote: > > > but I am sure it could be written in a cleaner way. > > > > ^Tnt(([^L]|.[^X]).+) > > Hmmm... It doesn't look very clean. It may be more correct but I am not sure > of that. It is not very clean. It does not match TntL and TntL1. > Does ?[^L]|.[^X] ?mean: > anything but 'L' or anything, followed by anything but 'X' ? The pipe is like an 'or' operator. [^L]|.[^X] means: Any character but L OR any character followed by any character but X. It matches a, b, La, Lb, but not LX and not L. Even better regexp is: ^Tnt(([^L]|L[^X]).*|L$)

The correct (and clean) answer is actually:
^Tnt(?!LX)(.+)$

Sadly, assertions are poorly supported by many languages/libraries. Use Perl if you want the full power of regexps ;-)

Alexander S. Klenin

Reply by email

Jürgen Hestermann
to Hans-Peter Diettrich
Hans-Peter Diettrich schrieb: > ^Tnt(([^L]|L[^X]).*|L$) I wonder who will be able to understand and update such expressions, at any later time. To me it looks like typical write-only code :-(

Yes, I also never got accustomed to regex syntax.
But I know that some are using it frequently.
And only then you will be able to manage such
special character monsters I think.

Reply by email

Juha Manninen
to Mattias Gaertner: On Tue, 15 Jun 2010 15:21:52 +0300 It is not very clean. It does not match TntL and TntL1.
Even better regexp is: ^Tnt(([^L]|L[^X]).*|L$)

Thanks.
In this case even my original regexp may be good enough. There are always many characters following "LX". This was a corner case anyway when trying to convert Tnt* 3rd party components to LCL fall-back components.

The fall-back rules system now works nicely. Replacements and regexp rules can be added and they are stored in a config file.

Juha

Reply by email

Henry Vermaak
to Hans-Peter Diettrich: I wonder who will be able to understand and update such expressions, at any later time.…
Hans-Peter Diettrich wrote 8 lines
Mattias Gaertner schrieb: > Even better regexp is: > > ^Tnt(([^L]|L[^X]).*|L$) I wonder who will be able to understand and update such expressions, at any later time. To me it looks like typical write-only code :-(

I'd wager that it's easier to understand than the equivalent pascal code that accomplishes the same function.

Henry

Reply by email