0

How can I remove the last ';' in a string?

In case of comment at the end of the string I need to return the ';' before the comment.

Example:

"line 1 //comment
line2;
extra text; //comment may also contain ;." 
9
  • Have you tried anything so far? If so please let us see the Regex Commented Mar 13, 2019 at 15:00
  • can't you just .Split(';') and then work with the array os strings? Commented Mar 13, 2019 at 15:02
  • 1
    Could you please update your question with expected output Commented Mar 13, 2019 at 15:04
  • @PaulF this is a non-trivial question. The regex would have to recognize and discard the comments Commented Mar 13, 2019 at 15:06
  • @JoãoPauloAmorim String.Split isn't "just", it generates new temporary strings for each operation that need to be garbage collected. A regex can be several orders of magnitude faster simply because it doesn't generate temporary strings. Commented Mar 13, 2019 at 15:07

2 Answers 2

1

You didn't wrote what you wanna do with the character, so I give you a solution here that replaces the character:

string pattern = "(?<!//.*);(?=[^;]*(//|$))";
Console.WriteLine(Regex.Replace("line 1 //comment", pattern, "#"));
Console.WriteLine(Regex.Replace("line2;", pattern, "#"));
Console.WriteLine(Regex.Replace("extra; text; //comment may also contain ;.", pattern, "#"));

Output:

line 1 //comment
line2#
extra; text# //comment may also contain ;.
Sign up to request clarification or add additional context in comments.

2 Comments

Note that if you have two semicolons in a line this replaces them both, which is not what the OP asked for.
@canton7 many thanks for your hint, you're completely right! I updated the answer with an extended RegEx that only replaces the last semicolon.
0

This is slightly ugly with Regex, but here it is:

var str = @"line 1 //comment

line2; test;

extra text; //comment may also contain ;.";

var matches = Regex.Matches(str, @"^(?:(?<!//).)+(;)", RegexOptions.Multiline);
if (matches.Count > 0)
{
    Console.WriteLine(matches[matches.Count - 1].Groups[1].Index);
}

We get a match for the last semicolon in each line (that isn't preceded by a comment), then we look at the last of these matches.

We have to do this on a line-by-line basis, as comments apply for the whole line.

If you want to process each line individually (your question doesn't say this, but it implies it), then loop over matches instead of just looking at the last one.

If you want to replace each semicolon with another character, then you can do something like this:

const string replacement = "#";
var result = Regex.Replace(str, @"^((?:(?<!//).)+);", "$1" + replacement, RegexOptions.Multiline);

If you want to remove it entirely, then simply:

var result = Regex.Replace(str, @"^((?:(?<!//).)+);", "$1", RegexOptions.Multiline);

If you just want to remove the final semicolon in the entire string, then you can just use string.Remove:

var matches = Regex.Matches(str, @"^(?:(?<!//).)+(;)", RegexOptions.Multiline);
if (matches.Count > 0)
{
    str = str.Remove(matches[matches.Count - 1].Groups[1].Index, 1);
}

1 Comment

How Can I remove this char?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.