1

I have a regex which is working fine in Javascript

var dbId = "Test/Detail[@Name='ok'][@Details='ok2']/Submit";
dbId.replace(/(?:\[@Name='.+?'\]|\[@Details='.+?'\])/g,"");

But when i am trying it in C# it is not working

C#

Regex.Replace(dbId, @"(?:\[@Name='.+?'\]|\[@Details='.+?'\])", String.Empty);

Please help

3
  • Why do you have the @ outside your string in the C# code, this is used in the method docs only some of the time, and the reasoning is not explained. It could be causing your issues. msdn.microsoft.com/en-us/library/e7f5w83z.aspx Commented Jul 30, 2013 at 15:46
  • Help us to help you. What is not working? Are you getting compilation error? Not behaving right? Explain the problem. Provide error messages. Commented Jul 30, 2013 at 15:47
  • 3
    @Robadob, @ creates a string literal, meaning anything inside the quotes is a valid character. It saves you escaping chars. Commented Jul 30, 2013 at 15:49

1 Answer 1

12

Strings are immutable, so replacement isn't done in place.

dbId = Regex.Replace(dbId, @"(?:\[@Name='.+?'\]|\[@Details='.+?'\])", String.Empty);

Assign the result back to dbId


Result: Test/Detail/Submit

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

2 Comments

+1, You don't know how many times I've done this exact same thing.
Thanks for the help it works!! As i read the reply from 14V banged my head to the desk.

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.