0

i have a string

string findText = "Paid part-time job (under 8 hours per week)";

i want to replace this string with some other string (replaceText = "Paid for job") using Regex.Replace() as follows :

Regex r = new Regex(findText, RegexOptions.IgnoreCase);
findText = r.Replace(findText, replaceText);

but the problem is that this string is not getting replaced, may be due to presence of braces in the text.

Can anyone suggest how this whole string can be replaced using Regex.Replace()?

2
  • 1
    So you want to replace "part-time" with "for"? Commented Feb 20, 2013 at 12:11
  • No i want to replace the whole string with "Paid for Job" or any other string. what i want is that i should be able to replace the whole string even if it contains special characters. Due to special characters , it is not getting replaced using regexOptions Commented Feb 20, 2013 at 12:51

2 Answers 2

1

There is no need at all for the usage of regular expressions here.
Simply use string.Replace:

var result = original.Replace(findText, replaceText);
Sign up to request clarification or add additional context in comments.

1 Comment

actually i need to use regular expressions here bcoz i have to consider the case sensitivity case here.
0

For your example it is easier to use String Replace:

string result = yourString.Replace(findText, replaceText);

But if you still want to use regex, you should escape braces, for example this way:

string findText = "Paid part-time job [(]under 8 hours per week[)]";

and call it pattern instead of findText :)

1 Comment

actually this findText is coming from some database, so cannot escape the braces...

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.