0

I want to parse a SQL statement which may use a SQL function inside the query text. Now using C# regex I'd like to parse the string and check if there is any function name(s) used inside the query and extract those. There may be multiple functions used inside the query and each function can have n number of parameters which is not known from before hand.

select * from fnTest(@A, @B) 
select x.* from fnTest(@A, @B) x join fnHelloWord(@C, @D, @E) y on x.SomeID = y.OtherID
  • In the first sample the regex would return a string value fnTest.
  • In the second sample the regex would return a list of function name strings like fnTest and fnHelloWord

3 Answers 3

4

If your string are like those you showed, then you can use a regex like this:

\w+(?=\()

Working demo

enter image description here

Btw, if you want to grab the regex content use capturing groups like:

(\w+)(?=\()

Match information:

MATCH 1
1.  [14-20] `fnTest`
MATCH 2
1.  [46-52] `fnTest`
MATCH 3
1.  [68-79] `fnHelloWord`
Sign up to request clarification or add additional context in comments.

Comments

2

To add to Fede's answer, You may also need to allow for some number of whitespaces between the function name and the open parenthesis.

e.g. \w+\s*(?=\()

Comments

1

Use this regex:

@"([a-zA-Z0-9_]*)\([^\)]*\)"

Basically ([a-zA-Z0-9]*) matches an identifier (a-z, A-Z, or 0-9) and captures it, \( matches a left parenthesis, [^\)]* matches any number of things which are not right parentheses, and then finally \) matches a right parenthesis.

Alternatively, you could use this regex:

@"(\w*)\([^\)]*\)"

Becaue \w matches [a-zA-Z0-9_].

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.