I’m currently writing a parser for ColdFusion code. I’m using a regex (in c#) to extract the name datasource attribute of the cfquery tag.
For the time being the regex is the following
<cfquery\s.*datasource\s*=\s*(?:'|")(.*)(?:'|")
it works well for strings like
<cfquery datasource="myDS"
or
<cfquery datasource='myDS'
But it gets crazy when parsing strings like
<cfquery datasource="#GetSourceName('myDS')#"
Obviously the part of the regex (?:’|”) is the cause. Is there a way to only match single quote when the first match was a single quote? And only match the double quote when the first match was a double quote?
Thanks in advance!
Edit: I think this should work in C# you just need to do a back reference:
or perhaps
matches
datasource="#GetSourceName('myDS')#"with a back reference to the first match with\1.Of course, you cannot ignore the first capture group with
?:and still have this work. Also, you may want to set thelazyflag so as not to match additional"‘s