0

I want to extract a content inside some function such as SUM, AVG, COUNT of a SQL query expression in PHP. For example SUM(A+B) -> A+B or SUM(SUM(A)) -> SUM(A) and A.

I have used a function preg_match_all with a regular expression as follows:

#(SUM|sum|AVG|avg|MIN|min|MAX|max|COUNT|count)\((.*?)\)#

It works very well with a single function such as SUM(A), but not for a multiple function case SUM(SUM(A)). The result of SUM(SUM(A)) is SUM(A. It looks like preg_match_all only recognizes the first ) and skip others next ).

Is there any way or any function help me to extract the content of the function in multiple function case?

mickmackusa
  • 33,121
  • 11
  • 58
  • 86
Duy Huynh
  • 241
  • 1
  • 12
  • 1
    cut your conditional size in half by using a case-insensitive pattern. So how much have your researched about recursive patterns? Please offer an sample input string that contains multiple "matchable" substrings and state your exact desired output. Please show your coding attempt. – mickmackusa Dec 18 '18 at 07:20
  • 1
    Use `/(sum|avg|min|max|count)\((.*)\)/i` https://regex101.com/r/zu1tjU/1/ – Mohammad Dec 18 '18 at 07:21
  • @Mohammad Please never post solutions as comments. p.s. there is no recursion in your pattern. (please remove) – mickmackusa Dec 18 '18 at 07:22
  • 2
    Your question is Unclear and incomplete. Please improve your question. Search this site: https://stackoverflow.com/questions/tagged/php+regex+recursion – mickmackusa Dec 18 '18 at 07:25
  • Thanks for reminding me that. I am new with regex. By the way, thank you all. I have found the answer in your comments. – Duy Huynh Dec 18 '18 at 07:27
  • @mickmackusa I think that is enough for me to find an answer. Thank you. How can I close this topic? – Duy Huynh Dec 18 '18 at 07:32
  • You are able to withdraw your question. – mickmackusa Dec 18 '18 at 07:33

0 Answers0