regex - Regular Expression to get a string between backtick `` in Console application (C#) -


i trying parse sql using regular expression in order return aliases (a string after keyword as between backtick ``). instance input

 var test = "select t.`productid` `productid`, t.`attributeid` `attributeid`, t.`string_value` `string_value`, t.`numeric_value` `numeric_value`, t.`metadata` `metadata`, t.`datatype` `datatype`, t.`createdtime` `createdtime`, t.`createdby` `createdby`, t.`modifiedtime` `modifiedtime`, t.`modifiedby` dfsfile.tmp1.my_json t"; 

the expected return is

"productid", "attributeid", "string_value", "numeric_value", "metadata", "datatype", "createdtime", "createdby", "modifiedtime" 

something (regular expression , linq):

 string test = "select t.`productid` `productid`, t.`attributeid` ...";   // if want preserve `` pattern @"\bas\s*(`[^`]*?`)"  string pattern = @"\bas\s*`([^`]*?)`";        var result = regex     .matches(test, pattern, regexoptions.ignorecase)     .oftype<match>()     .select(match => match.groups[1].value)     .toarray(); // if want, say, array representation   console.write(string.join(", ", result)); 

and you'll get

  productid, attributeid, ... , modifiedby 

however, careful: in general case regular expressions not good choice parsing sql; let me provide examples show problems emerging:

  -- commented ("abc" should not returned)   select /* `abc`*/     tbl     -- commented value ("abc" should returned, not "obsolete" or "proposed")   select /*`obsolete`*/ `abc` /*`proposed`*/     tbl     -- string ("abc" should not returned)   select 'a `abc`'     tbl    -- honest ("abc" should returned)   select /*'*/as `abc`--'     tbl    -- commented comment ("abc" should returned)   select -- /*         `abc`         --*/    tbl    

Comments

Popular posts from this blog

java - Jasper subreport showing only one entry from the JSON data source when embedded in the Title band -

serialization - Convert Any type in scala to Array[Byte] and back -

SonarQube Plugin for Jenkins does not find SonarQube Scanner executable -