1

I am using a Regex to find a pattern within a string. After I find a match I would then like to remove it from the existing string.

For example:

starting string ex: "Billed Hours (.5);" 
converted string: "Billed Hours"

The regex pattern I am using is @"([a-z.]+);$" - Not sure if this is correct, I want to check that only whole numbers or decimals are within the parentheses. If the pattern matches then remove parentheses, the value inside as well as the following semi-colon.

This is what I have so far:

string testString = "Billed Hours (.5);"
testString = Regex.Replace(testString, @"\([a-z.]+\);$", "");

This is in C#.

Any ideas?

2
  • You're currently missing digits. Commented Sep 26, 2013 at 20:05
  • What language are you using? Commented Sep 26, 2013 at 20:06

2 Answers 2

1

Try the regex:

@"\s+\([^)]+\);$"

And replace by nothing.

The \s+ is to avoid having to trim spaces and [^)]+ matches any character except closing paren.

Sign up to request clarification or add additional context in comments.

3 Comments

Jerry so you are saying I should try this? string newString = Regex.Replace(testString, @"\s+([^)]+);$", ""); I still get nothing replaced and the same original string.
@Aaron Uh, that's not the regex I suggested... I meant this: string newString = Regex.Replace(testString, @"\s+\([^)]+\);$", "");
Posted the wrong Regex in my reply. Everything is working thanks Jerry.
1

Consider...

string testString= Regex.Replace(testString, @"\(\d*\.?\d*]*\);$", "");

1 Comment

The string is identical and not removing the parentheses, value within or the semi-colon. Wondering what I'm doing wrong....

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.