I want to use Regular Expressions to extract information from my source code. Can you help me to build a RegEx that retrieves the units used on the source code ?.
Source code sample:
unit ComandesVendes;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Manteniment;
type
TFComandesVendes = class(TFManteniment,ActualitzacioFinestra)
QRCapsaleraNumero: TIntegerField;
QRCapsaleraData: TDateTimeField;
QRCapsaleraDataEntrega: TDateTimeField;
...
...
I need to get the comma-separated file names since the uses clause up to the next ;. In that sample the output must be:
Windows
Messages
SysUtils
Variants
Classes
Graphics
Controls
Forms
Dialogs
Manteniment
I'm trying something like
^ *uses(\n* *(\w*),)* *\n* *(\w*) *;
It matches the uses clause, but it doesn't return each file name separately.
Thank you.
At this page it says that Delphi uses the PCRE regex flavor.
In that case, one option is to use a capturing group in combination with the \G anchor.
(?:^ *uses\r?\n *|\G(?!^))(\w+)(?:,\s*|;$)
Explanation
(?: Non capture group
^ *uses\r?\n * Match optional spaces from the start of the string, then match and a newline followed by optional spaces again| Or\G(?!^) Assert the position at the end of the previous match, not at the start (The \G anchor matches at 2 positions, either at the start of the string or at the end of the previous match)) Close non capture group(\w+) Capture group 1 Match 1+ word characters(?:,\s*|;$) Non capture group, match either a comma and 0+ whitespace chars or match ; at the end of the string.Regex demo
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With