Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matching "beginning-of-line" using libc++ regex library (C++11)

I would like to match all lines that start with a given word, say iheap. If I am not mistaken the regular expression (in ECMAScript syntax) "^iheap.*" should do the trick. However, when I tested this in C++11 using libc++'s regex library, only the first line is matched. So "^..." seems to only match beginning-of-input instead of beginning-of-line.

Here is an example:

#include <string>
#include <regex>
#include <iostream>

using namespace std;

int main() {
        regex rx("^iheap.*");
        string s = "iheap says hello.\niheap says hello again.\n";
        cout << s << regex_replace(s, rx, "IHEAP");
        return 0;
}

Output:

iheap says hello.
iheap says hello again.
IHEAP
iheap says hello again.

Is this a bug of libc++ or am I doing something wrong? Thanks!

Note: I am using Mac OS X Mountain Lion and Apple LLVM Compiler 4.0 (basically a snapshot of clang 3.1 SVN).

like image 455
iheap Avatar asked Aug 14 '12 20:08

iheap


1 Answers

I've looked over all of the relevant standards, and as far as I can tell, ^ matches only the beginning of the string, and not a newline, unless the engine is in multiline mode. The default engine is ECMA-262 The engine is not in multiline mode by default, and I see no way to put it into multiline mode using the std C++ interface.

All that being said, if someone can point me towards normative text that says differently, I'll consider this a bug report and do my best to fix it.

like image 179
Howard Hinnant Avatar answered Oct 26 '22 23:10

Howard Hinnant