开发者

Converting C# Regex into C++/CLI?

开发者 https://www.devze.com 2023-01-02 14:02 出处:网络
I\'m having trouble getting my C# Regex working for C++. In C# I h开发者_开发问答ave: //using System.Text.RegularExpressions;

I'm having trouble getting my C# Regex working for C++. In C# I h开发者_开发问答ave:

 //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.


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

Regex^ rx = gcnew Regex("?<name>\\w{3,16}", static_cast<RegexOptions>(RegexOptions::Compiled));
0

精彩评论

暂无评论...
验证码 换一张
取 消