Øyvind Isaksen
Øyvind Isaksen

Reputation: 195

Remove part of string between 2 brackets containing a specific word

I need to make a function called RemoveError, that checks if a string contains the word "Error" inside 2 brackets with other text. If so, I need to remove the 2 brackets sorrounding "Error" and everything inside it.

Example:

var Result = RemoveError("Lorem Ipsum (Status: Hello) (Error: 14) (Comment: Some text)");

Result will return: "Lorem Ipsum (Status: Hello) (Comment: Some text)"

Hope someone can help

Upvotes: 0

Views: 176

Answers (1)

Marco
Marco

Reputation: 23935

You could try this Regex pattern:

public string RemoveError(string input) {
    return Regex.Replace(input, @"\(Error\:\s[0-9]{1,3}\)\s", "");
}

I am assuming that your error code is numeric and between 1 and 3 digits long. If that is not the case, you need to adapt that part of the expression. I am additionally removing one extra whitespace after the error part, because otherwise you would end up with 2 whitespaces in between.

\( - opening paranthesis
Error - match the word Error
\: - match the colon
\s - match a whitespace
[0-9]{1,3} - match 1 to 3 characters in the range from 0-9
\) - match a closing paranthesis
\s - match a whitespace

Output:

Lorem Ipsum (Status: Hello) (Comment: Some text)

Upvotes: 3

Related Questions