views:

1535

answers:

2

I'm working on pl/sql code where i want to replace ';' which is commented with '~'.

e.g.

If i have a code as:

--comment 1 with;
select id from t_id;
--comment 2 with ;
select name from t_id;
/*comment 3 
with ;*/

Then i want my result text as:

--comment 1 with~
select id from t_id;
--comment 2 with ~
select name from t_id;
/*comment 3 
with ~*/

Can it be done using regex in C#?

+1  A: 

A regex is not really needed - you can iterate on lines, locate the lines starting with "--" and replace ";" with "~" on them.

String.StartsWith("--") - Determines whether the beginning of an instance of String matches a specified string.

String.Replace(";", "~") - Returns a new string in which all occurrences of a specified Unicode character or String in this instance are replaced with another specified Unicode character or String.

gimel
well, the thing is i'll have to split it on newline.and i dont want to since i have a code which is too large.Also, it can happen that for a code likeselect *from g;--select statementi will not be able to replace the ; in comment.
Archie
+4  A: 

Regular expression:

((?:--|/\*)[^~]*)~(\*/)?

C# code to use it:

string code = "all that text of yours";
Regex regex = new Regex(@"((?:--|/\*)[^~]*)~(\*/)?", RegexOptions.Multiline);
result = regex.Replace(code, "$1;$2");

Not tested with C#, but the regular expression and the replacement works in RegexBuddy with your text =)

Note: I am not a very brilliant regular expression writer, so it could probably have been written better. But it works. And handles both your cases with one-liner-comments starting with -- and also the multiline ones with /* */

Edit: Read your comment to the other answer, so removed the ^ anchor, so that it takes care of comments not starting on a new line as well.

Edit 2: Figured it could be simplified a bit. Also found it works fine without the ending $ anchor as well.

Explanation:

// ((?:--|/\*)[^~]*)~(\*/)?
// 
// Options: ^ and $ match at line breaks
// 
// Match the regular expression below and capture its match into backreference number 1 «((?:--|/\*)[^~]*)»
//    Match the regular expression below «(?:--|/\*)»
//       Match either the regular expression below (attempting the next alternative only if this one fails) «--»
//          Match the characters “--” literally «--»
//       Or match regular expression number 2 below (the entire group fails if this one fails to match) «/\*»
//          Match the character “/” literally «/»
//          Match the character “*” literally «\*»
//    Match any character that is NOT a “~” «[^~]*»
//       Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
// Match the character “~” literally «~»
// Match the regular expression below and capture its match into backreference number 2 «(\*/)?»
//    Between zero and one times, as many times as possible, giving back as needed (greedy) «?»
//    Match the character “*” literally «\*»
//    Match the character “/” literally «/»
Svish