Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add match_str_many #11

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions regex.v
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,43 @@ pub fn (r &Regex) match_str(str string, pos int, options int) !MatchData {
}
}



// like match_str, match_str_many returns an array of MatchData structure containing all matched strings
// useful for regex with multiple capture groups
pub fn (r &Regex) match_str_many(str string, pos int, options int) ![]MatchData {
if pos < 0 || pos >= str.len {
return error('Invalid position')
}
ovector_size := (r.captures + 1) * 3
ovector := []int{len: ovector_size}
mut ret := C.pcre_exec(r.re, r.extra, &char(str.str), str.len, pos, options, ovector.data,
ovector_size)
if ret <= 0 {
return error('No match!')
}
mut match_datas := []MatchData{}
for {
mut new_pos := ovector[1]
if ret <= 0 {
break
}
match_data := MatchData{
re: r.re
regex: r
str: str
ovector: ovector.clone()
pos: new_pos
group_size: r.captures + 1
}
match_datas << match_data
ret = C.pcre_exec(r.re, r.extra, &char(str.str), str.len, new_pos, options, ovector.data,
ovector_size)
}
return match_datas
}


// new_regex create a new regex
// * source: the string representing the regex
// * options: the options as mentioned in the PCRE documentation
Expand Down
Loading