How to match line-break in c++ regex?

The dot in regex usually matches any character other than a newline std::ECMAScript syntax.

.   not newline   any character except line terminators (LF, CR, LS, PS).

0s7fg9078dfg09d78fg097dsfg7sdg\r\nfdfgdfg
[a-zA-Z0-9]+ matches until \r ↑___↑ .* would match from here

In many regex flavors there is a dotall flag available to make the dot also match newlines.

If not, there are workarounds in different languages such as [^] not nothing or [\S\s] any whitespace or non-whitespace together in a class wich results in any character including \n

regex_string = "([a-zA-Z0-9]+)[\\S\\s]*";

Or use optional line breaks: ([a-zA-Z0-9]+).*(?:\\r?\\n.*)* or ([a-zA-Z0-9]+)(?:.|\\r?\\n)*

See your updated demo


Update - Another idea worth mentioning: std::regex::extended

A <period> ( '.' ), when used outside a bracket expression, is an ERE that shall match any character in the supported character set except NUL.

std::regex rgx(regex_string, std::regex::extended);

See this demo at tio.run


You may try const static char * regex_string = "((.|\r\n)*)"; I hope It will help you.


I am using CTest and PROPERTIES PASS_REGULAR_EXPRESSION.

[\S\s]* did not work, but (.|\r|\n)* did.

This regex:

Function registered for ID 2 was called(.|\r|\n)*PASS

Matches:

Running test function: RegisterThreeDiffItemsTest04
ID 2 registered for callback
ID 4 registered for callback
ID 11 registered for callback
Function registered for ID 2 was called
ID 2 callback deregistered
ID 4 callback deregistered
ID 11 callback deregistered
Setup: PASS

Note: CMakeLists.txt needs to escape the backslashes:

SET (ANDPASS "(.|\\r|\\n)*PASS")

Tags:

C++

Regex