CShark
CShark

Reputation: 33

How to parse the following with C# regex

I have this text: "((FIELD1_NAME like 'Product%') And (Instr(FIELD1_NAME, 'Product_typ') = 1))";

I would like to parse the FIELD1_NAME and Product_typ from the Instr part of the string.

This didn't work: @"Instr((?<column>[A-Z0-9_]+), '(?<value>([^']|(''))+)'"

Upvotes: 0

Views: 63

Answers (2)

user557597
user557597

Reputation:

This regex uses dot-all on a lazy quantifiers to get the var's.
Mostly to trim and because you don't state it's for validation.

 # @"(?s)Instr\s*\(\s*(?<feild>.*?)\s*,\s*'\s*(?<prodtype>.*?)\s*'\s*\)"

 (?s)
 Instr
 \s* 
 \(
 \s* 
 (?<feild> .*? )               #_(1)         
 \s* , \s* 
 '
 \s* 
 (?<prodtype> .*? )            #_(2)         
 \s* 
 '
 \s* 
 \)

Upvotes: 0

Avinash Raj
Avinash Raj

Reputation: 174706

You need to escape the first ( after Instr and also make the group inside value group to non-capturing group,

Instr\((?<column>[A-Z0-9_]+),\s*'(?<value>(?:[^']|(''))+)'

DEMO

Upvotes: 2

Related Questions