dongzai3917 2013-12-10 20:03
浏览 9
已采纳

golang删除数组的维数

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

  • 写回答

1条回答 默认 最新

  • 普通网友 2013-12-10 20:15
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大