0

I'm having trouble getting my C# Regex working for C++. In C# I have:

 //using System.Text.RegularExpressions;
 Regex YourName = new Regex("?<name>\w{3,16}");

but in C++ this does not correctly match:

 //using namespace System::Text::RegularExpressions;
 Regex^ rx = gcnew Regex("?<name>\w{3,16}", static_cast<RegexOptions>(RegexOptions::Compiled));

followed by:

 MatchCollection^ matches = rx->Matches( input ); //input=String^

Matches always return 0 count. Am I doing something really silly? Is there something special you need to do to convert C# regex into C++ regex? Many thanks for any light you can shed on this.

1
  • 1
    That's C#, not C. C has no regexes of any type. Commented Jun 4, 2010 at 20:11

1 Answer 1

3

You need to escape the \ from the compiler, like this:

Regex^ rx = gcnew Regex("?<name>\\w{3,16}", static_cast<RegexOptions>(RegexOptions::Compiled));
Sign up to request clarification or add additional context in comments.

2 Comments

Wouldn't he need to escape it in the C# one too? OP, are you sure your C# version doesn;'t have a literal string (starts with @"...")?
hah, yes you're right. Completely missed that. That does fix it though, thanks to both of you.

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.