我不能在C++的正則表達式中使用反向引用。 試圖更深奧的東西后,我試圖在GCC 4.8.1這個簡單的腳本:C++ 11正則表達式返回引用
#include <iostream>
#include <regex>
#include <string>
using namespace std;
int main() {
regex e("(..)\\1");
string s("aaaa");
if (regex_match(s,e))
cout << "match" << endl;
return 0;
}
,但它會產生一個運行時錯誤。我嘗試了regex_constants中的各種標誌,如ECMAScript
或grep
,但無濟於事。這種在C++ regex引擎中使用反向引用的方式有什麼問題?
只是爲了確保我是不是失去了一些東西小事,我在Java中
嘗試這種class TestIt
{
public static void main (String[] args) throws java.lang.Exception
{
final String s = "aaaa";
final String e = "(..)\\1";
if (s.matches(e))
System.out.printf("match");
}
};
,顯然它打印match
如預期,這是令人欣慰的。
只是要強調:他試圖在* search *模式中使用反向引用 - 而不是* replace *模式。這在一些正則表達式引擎中是有效的。 –
有沒有正確的正則表達式引擎? – UndefinedBehavior
libstdC++中的正則表達式工具不能正常工作。使用boost或其他正則表達式實現,直到它被修復,或者使用libC++。 –