I've a code like that in golang
func GetIndexes(body string) ([]int, error) {
indexPattern, err := regexp.Compile(`<div class="post article" id="([0-9]+)">`)
res := indexPattern.FindAllStringSubmatch(body, -1)
fmt.Printf("%v
", res)
//Just for debug
return make([]int, 5), err
}
for exemple the result is like :
[
[<div class="post article" id="55987"> 55987]
[<div class="post article" id="6717024"> 6717024]
[<div class="post article" id="6440542"> 6440542]
[<div class="post article" id="6800745"> 6800745]
[<div class="post article" id="449954"> 449954]
[<div class="post article" id="427586"> 427586]
[<div class="post article" id="5418445"> 5418445]
[<div class="post article" id="559225"> 559225]
...
]
And I'm looking for a way to get just an array like
[55987, 6717024, 6717024, ...]
I could range the array and copy value which I looking for, but i'm not sure it's the better way. It's why I ask myself if it's possible to drop column of this array, or why not create kind of slices with lambdas functions or other...
Thank you
This is more of a RegEx engine issue because the engine will output the results in the following format:
res[0] // will be the first matched "whole string" occurrence
res[0][0] // will be the whole string match
res[0][1] // will be the first grouped match -- the things in parenthesis
res[0]['some_name'] // will be a named group match
You have to do is iterate over res[i]
and retrieve res[i][1]
.
Since the RegEx you're trying to match may be very complex -- it may have many grouped matches, many named grouped matches etc. -- the result variable might also be fairly complex.
Because of the (possible) complexity of the result variable there would be no point for a RegEx library to provide you with functions that do exactly what you described because those functions would be of very limited use.
Also writing such a snippet of code or function is trivial task so you have to mix and match your own according to your very particular set of needs.