0

I am trying to retrieve a regex expression from database and pass it on to client (Browser). But the regex expression is getting slightly modified in between due to intermidiate parsing and regex expression processing in C#. Can anyone please tell me what I need to do inorder to pass the regex correctly to client. I have the following piece of code

private static readonly Regex defaultObjectPattern = new Regex(@"([^\w\.$\]])(\['?[$\w \s]*'?\][^\.\[])", RegexOptions.Compiled);

var parsedFormula = new StringBuilder(defaultObjectPattern.Replace(rule.Rule, "$1item$2"));

where rule.Rule is

"Error(\"![Item].[XYZ ID].match(/^20[^\s]{4,}$|^$/)\", \"Invaid XYZ ID\",\"XYZ ID entered is Invalid. Please obtain a valid XYZ ID from your Supervisor or delete this entry.\", \"XYZ ID\",\"All\")"

By the time above instruction is complete the regex expression in the match method is getting modified as /^20\['^\s']{4,}$|^$/. Single quotes are getting added within the Square brackets.

The exact string I am storing in the database is

'Error("![Item].[XYZ ID].match(/^20[^\s]{4,}$|^$/)", "Invaid XYZ ID","XYZ ID entered is Invalid. Please obtain a valid XYZ ID from your Supervisor or delete this entry.", "XYZ ID","All")'

I cannot change the defaultObjectPattern as it is used for lot of other things. But I need to get the regex expression in match method without getting modified (without single quotes getting added).

Thanks in advance for your help.

Cᴏʀʏ
  • 105,112
  • 20
  • 162
  • 194
C V
  • 209
  • 1
  • 3
  • 13
  • I copied your `defaultObjectPattern` regex into a regex tester and it doesn't seem to match anything in either of your "rule.Rule" strings. When you say, "the regex expression in the match method is getting modified as ...", you mean that `parsedFormula` contains the modified string you are talking about? Also I don't see how "database" or "sql-server" fit into the question as I see no database code at all. – Quantic Mar 15 '16 at 15:45
  • yes. Parsed formula contains the modified string. Entire rule.Rule is coming from database. I mentioned in the my post how I am saving that in the database. Thanks – C V Mar 16 '16 at 20:01
  • I can't compile the code because you say "rule.Rule is", but that string will not compile as there is a missing escape character exactly in the region you are having problems with, `[^\s]`. The only way I can compile your code is to add an escape character, `[^\\s]`, but doing this does *not* add single quotes when running regex.replace. So I can't even test your code because it doesn't compile, basically. Can you copy and paste the *actual* rule.Rule from a debugger instead of wherever you are pasting it from right now? Then I can try and at least duplicate your problem. – Quantic Mar 16 '16 at 20:12
  • String I am storing the database: Error("![DealItems].[CNDC ID].match(/^20[^\\s]{4,}$|^$/)", "Invaid CNDC ID","CNDC ID entered is Invalid. Please obtain a valid CNDC ID from your Sales Engineer or delete this line component.", "CNDC ID","All") – C V Mar 16 '16 at 23:49
  • After escaping all the quotes in your last comment, seen here: `"Error(\"![DealItems].[CNDC ID].match(/^ 20[^\\s]{ 4,}$|^$/)\", \"Invaid CNDC ID\",\"CNDC ID entered is Invalid.Please obtain a valid CNDC ID from your Sales Engineer or delete this line component.\", \"CNDC ID\",\"All\")"`, I ran your code and it *does not* add single quotes within the square brackets: `parsedForumla = {Error("![DealItems].[CNDC ID].match(/^ 20[^\s]{ 4,}$|^$/)", "Invaid CNDC ID","CNDC ID entered is Invalid.Please obtain a valid CNDC ID from your Sales Engineer or delete this line component.", "CNDC ID","All")}` – Quantic Mar 17 '16 at 14:48

0 Answers0