0

I am trying to parse a SQL String and look for SQL Template Parameter formatted strings. i.e. < Name, Type, Value>. I am parsing that data out to a Dictionary<String, Object>. I am not an expert in Regex, but this appears to be the best way to find the specific format, I just cannot seem to wrap my head around the Regex command I am looking for.

<.+?>

Will get me the Template Parameters I am looking for, but also catches part of a common Where statement of

WHERE (Column < value) or (column > value2)

What would be the Regex command to find all instances of "<string,string,stringornumber>" everything I tried fails to find anything, so I am sure I am just not fully grasping Regex for some reason today.

0

2 Answers 2

2

This should work for you, this is a javascript example but you can change it to C#, if you need to account for other characters besides a-zA-Z0-9@_ then just add those characters into the bracket expressions:

const mysql = 'select <@test, int,  10>, <@test2, bigint, 1200000000000> from mytable where (col1 < @test) or (col1 > @test2)';
const matches = mysql.match(/<[a-zA-Z0-9@_]+?,\s*?[a-zA-Z0-9@_]+?,\s*?[a-zA-Z0-9@_]+>/g);
for(let a = 0; a < matches.length; a++){
   console.log(matches[a]);
}

EDIT

Here's a C# Example:

Regex reg = new Regex("<[a-zA-Z0-9@_]+?,\\s*?[a-zA-Z0-9@_]+?,\\s*?[a-zA-Z0-9@_]+>", RegexOptions.Multiline);        
string test = "select <@test, int,  10>, <@test2, bigint, 1200000000000> from mytable where (col1 < @test) or (col1 > @test2)";
MatchCollection matches = reg.Matches(test);
foreach(Match m in matches)
{
    Console.WriteLine(m.Value);
}
Sign up to request clarification or add additional context in comments.

1 Comment

So, I see someone downvoted this. Want to explain why? Or are you just going through and down voting people?
1

For a broad match, you can match non whitespace characters except , < > using a negated character class:

<(?:[^\s<>,]+\s*,\s*){2}[^\s<>,]+>

The pattern matches:

  • < Match literally
  • (?: Non capture group
    • [^\s<>,]+ Match 1+ non whitespace chars other than < > and ,
    • \s*,\s* Match a comma between optional whitespace chars
  • ){2} Close the non capture group and repeat 2 times
  • [^\s<>,]+ Match 1+ non whitespace chars other than < > and ,
  • > Match literally

See a regex demo.


If only the first value can start with an @ you can make the pattern stricter and start with match with @ and 1 or more word characters \w+ and only word characters for the 2nd and 3rd value:

<@\w+\s*,\s*\w+\s*,\s*\w+>

Regex demo

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.