hanickadot / compile-time-regular-expressions

Compile Time Regular Expression in C++
https://twitter.com/hankadusikova
Apache License 2.0
3.22k stars 177 forks source link

global flag support ? #260

Closed MrinmoyHaloi closed 2 years ago

MrinmoyHaloi commented 2 years ago

I want to know how can i get all occurrences of a search. If i use normal search functions it only gives the first occurrence.

My code:

std::cout<<ctre::multiline_search<"func.*">(str);

str is a file with the following content:

func name(){
    fdjfkla
}

func hello(){
    print("hello")
}

The code currently outputs this: func name(){%

What i am expecting:

func name(){%
func hello(){%

I have tested the regex in javascript with the global flag and it works perfectly.

hanickadot commented 2 years ago

ctre::search, ctre::starts_with, ctre::match and their multiline variants returns only one set of captures

to obtain multiple, you need to use other API (or multiline variants) ctre::range<"pattern"> ... return forward iterable range which is equivalent of multiple calls of ctre::search (each time on remainder of the result) ctre::tokenize<"pattern"> ... equivalent to ctre::starts_with multiple time (same as with ctre::range)

there are also ctre::iterator<"pattern"> which returns directly the iterator for matching (same as one obtained from .begin() in ctre::range result) and ctre::split<"pattern"> which returns range of ranges delimited by the pattern

example for ctre::range: https://compiler-explorer.com/z/vn5x8b1zf