Dib
Dib

Reputation: 2083

C# Regular Expression regex to match characters in last group, but not empty group

Please can any one advise me of a solution that will allow my regular expression to match the following data lines...

0002,Area (Region Name),No value
0003,Area (Region Name),Up North

...but should fail on this line (due to the empty last group)

0001,Area (Region Name),

My current expression is:

(?<BRANCH_CODE>[a-zA-Z0-9]{1,8}),(?<UDA_NAME>[a-zA-Z0-9!"\£\$\%\^\&\*\(\)\-+_ /]{1,50}),(?<UDA_VALUE>[a-zA-Z0-9!"\£\$\%\^\&\*\(\)\-+_ /]{1,50})

... which works fine in Expresso, but NOT in my C# code!

Any help appreciated. Thank you.

Reference that has not helped: Regular expression matches an extra empty group

Upvotes: 0

Views: 121

Answers (2)

Avinash Raj
Avinash Raj

Reputation: 174706

Escape all the double quotes .

@"(?<BRANCH_CODE>[a-zA-Z0-9]{1,8}),(?<UDA_NAME>[a-zA-Z0-9!""\£\$\%\^\&\*\(\)\-+_ /]{1,50}),(?<UDA_VALUE>[a-zA-Z0-9!""\£\$\%\^\&\*\(\)\-+_ /]{1,50})"

Upvotes: 3

Richard
Richard

Reputation: 30618

The expression looks correct, but I would ensure you've got a start and end character on your expression, or it might match within a string.

^(?<BRANCH_CODE>[a-zA-Z0-9]{1,8}),(?<UDA_NAME>[a-zA-Z0-9!"\£\$\%\^\&\*\(\)\-+_ /]{1,50}),(?<UDA_VALUE>[a-zA-Z0-9!"\£\$\%\^\&\*\(\)\-+_ /]{1,50})$

Upvotes: 1

Related Questions