developer82
developer82

Reputation: 13713

Regular Expressions get matches without match start char

I have the following regex expression:

Regex regex = new Regex(@"@([\w\-]+)");

What it does is to find matches of parameters in SQL queries. For instance for the following input

select * from some_table where fieldA = @FieldA and fieldB = @FieldB

the result will be @FieldA and @FieldB

But what I actually want is just "FieldA" and "FieldB" - without the @ sign. Is this possible in the regex itself or just in code?

Upvotes: 1

Views: 30

Answers (1)

anubhava
anubhava

Reputation: 785186

You can use lookbehind so that your regex doesn't capture @ in matched group:

Regex regex = new Regex(@"(?<=@)([\w-]+)");

Here (?<=@) is a positive lookbehind, that ensures your match is preceded by a @

Upvotes: 1

Related Questions