简体   繁体   English

golang删除数组的维数

[英]golang drop a dimension of array

I've a code like that in golang 我在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\n", 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... 这就是为什么我问自己是否可以删除此数组的列,或者为什么不使用lambdas函数或其他函数创建某种切片的原因...

Thank you 谢谢

This is more of a RegEx engine issue because the engine will output the results in the following format: 这更多是RegEx引擎问题,因为引擎将以以下格式输出结果:

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] . 您要做的是遍历res[i]并检索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. 由于您要匹配的RegEx可能非常复杂-它可能具有许多分组匹配,许多命名分组匹配等-结果变量也可能相当复杂。

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. 由于结果变量的(可能的)复杂性,RegEx库没有必要向您提供与您描述的功能完全相同的功能,因为这些功能的用途非常有限。

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. 同样,编写这样的代码段或函数段也是微不足道的任务,因此您必须根据非常特殊的需求组合和匹配自己的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM