1

I was wondering what is the best way to include backslash and other special characters in a group?

example:

"message":"\"rock on\" \\,,/,[-_-]";

help me on my regex

[a-zA-Z0-9 \\-~!@#$%^*()_+{}:|?`;',\\./\\[\\]]+
1
  • can you elaborate on what you're trying to do? what exactly is the "example" you've given, and what do you want to do with the regex? What's the goal? Commented May 2, 2011 at 14:12

1 Answer 1

6

Just escape those that need to be escaped and add those, that don't need to:

[a-zA-Z0-9 \\\-~!@#$%^*()_+{}:|"?`;',./[\]]+

To elaborate a bit:
You only need to escape \, ] and - inside a character group.

Using C#, it would look like this:

Regex rx = new Regex(@"[a-zA-Z0-9 \\\-~!@#$%^*()_+{}:|""?`;',./[\]]+");
Sign up to request clarification or add additional context in comments.

3 Comments

The quote should work, too. I updated my answer with a C# sample.
doesn't matter. the quote was passed as \x26amp; I'll just have to replace
@RaghavKhunger: Correct, I hit the wrong key when posting this. You have to escape ] and not [.

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.