正则表达式匹配三种模式

时间:2015-03-12 12:19:18

标签: c++ regex

我有一个字符串数组,其中元素可以是以下模式之一:

  • url www.example.com
  • hashtag #followback
  • normal currentratesoughttogodown

根据输出examplefollowbackcurrentratesoughttogodown创建。

我目前的正则表达式

(?<=www.|\\#)(\\w+)(?=\\.)?

在Java中匹配www.example.com#followback但在C++中匹配,因为C++不支持lookbehind。

如何在c ++中匹配这三种模式?

1 个答案:

答案 0 :(得分:0)

您可以改用捕获组。像这样的字符串将驻留在第一个捕获组中:

smatch sm1;
string s1("www.example.com");
regex_search(s1, sm1, regex("^(?:www\\.|\\#)?(.+?)(?:\\.[^.]+?)?$"));
cout << sm1[1] << endl;

请参阅C++ regex string capture

相关问题