c# - Regex replace in sql grouping -


i want replace in sql grouping condition fields casted version of fields:

for example:

input: sum(count(andrei) + count(2) + sum(count3)  ouptput: sum(count(cast(andrei int)) + count(cast(2 int)) + sum(cast(count3 int)) 

my idea find literals contain no "(" or ")" following pattern:

  match m = regex.match(input, "\\([^\\(\\)]+\\)"); 

and replace them casted version.

i don't know how perform replace.

you use following pattern , replacement string.

pattern: (?<=\()([^()]+)(?=\))

  • (?<=\(): look-behind matches (but doesn't consume) opening parenthesis
  • ([^()]+): numbered capture group negative character class match except opening , closing parentheses. there's no need escape them when appear inside character class.
  • (?=\)): look-ahead detects closing parenthesis

replacement string: cast($1 int) $1 refers first numbered capture group

string input = "sum(count(andrei)) + count(2) + sum(count3)"; string pattern = @"(?<=\()([^()]+)(?=\))"; string replacement = "cast($1 int)"; string result = regex.replace(input, pattern, replacement); console.writeline(result); 

Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -