gsharp
gsharp

Reputation: 27927

Extract all Parameters with RegEx

I have the following SQL sample statement in a string variable

INSERT INTO T_Application
(
    ApplicationGroupId,
    Name,
    Component,
    SubComponent,
    Description
)
VALUES
(
    @ApplicationGroupId,
    @Name,
    @Component,
    @SubComponent,
    @Description
)

SET @Id = SCOPE_IDENTITY()

What I want is to have all Parameter names (Id, ApplicationGroupId, Name, Component, SubComponent, Description) in a List<string>. How can I write the RegEx to extract all Parameters Names? The RegEx should also work if the Parameters are in one line, spaces between , etc.

Upvotes: 4

Views: 2331

Answers (3)

Shekhar
Shekhar

Reputation: 11788

You can use following regular expression :

insert into[\s\S]+?\((?<parameter_names>([\s\S]+?))\)

You will get all the parameters in group named 'parameter_names'.

Upvotes: 0

Francis Gilbert
Francis Gilbert

Reputation: 3442

You could try this regex:

\({0,1}\s*(\w*)\,

You can then get the matches using flags

Upvotes: 0

agent-j
agent-j

Reputation: 27913

Regex.Matches(sql, @"\@\w+").Cast<Match>().Select(m => m.Value).ToList ();

Resulting List:

@ApplicationGroupId
@Name
@Component
@SubComponent
@Description   

Upvotes: 10

Related Questions