Bit of regex help, if you would (w/PHP)

  • Thread starter Thread starter Sic
  • Start date Start date

Sic

Sic

Soldato
Joined
9 Nov 2004
Posts
15,365
Location
SO16
Bonjour,

having a bit of a headache with a regex - if anyone could help, that'd be great. I'm using the following to check if a string matches the format of a MySQL function. This is proving to be a bit of a pain because of the ability to nest functions in MySQL.

I'm currently using the following:

Code:
[A-Za-z0-9_]+\([^)]*\)

that matches things like NOW() and DATEDIFF('1997-12-31 23:59:59','1997-12-30'), but it doesn't match DATEDIFF(NOW(),'1997-12-30')

does anyone have any idea how I might achieve this using a regex?

Furthermore, is there a way I can check whether the pattern matched the whole string, rather than just a part of it? would that just be a case of checking the match[0] argument's length compared with the string I'm checking's length?

merci :)
 
Not 100% sure if this is supported in PHP, but in DotNet you can count matched:
Code:
\w+ # Method.
\( # First bracket.
(?<functionCall>(?>[^()]+ | \( (?<number>) | \) (?<-number>))*) # Capture all chars till bracket count is zero. Increments when open bracket found.
\) # Last bracket.

Not sure I understood your last point :)
 
Not great with regex [read: crap] but could you not loop through the matches?

So if it matches say DATEDIFF(), have the function apply the pattern to anything inside the brackets?

I'm not 100% sure what you are after so I could be way off.
 
datediff was just an example - there are other mysql functions that allow you to nest other functions in there - I don't really want to have to do any looping if I don't have to. I'm pretty sure regex must be able to handle this on its own, but I don't really know enough about them to attempt it. I'll try what you've posted there Goksly - don't know if php supports counting matched either.

thanks :)
 
Back
Top Bottom