3

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.

Marc Guillot
  • 5,367
  • 11
  • 30

1 Answers1

6

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

The fourth bird
  • 96,715
  • 14
  • 35
  • 52