Golang删除数组的一个维度。

huangapple go评论107阅读模式
英文:

golang drop a dimension of array

问题

我可以帮你翻译这段代码。这是一个使用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
}

例如,结果可能是这样的:

[ 
    [<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]
    ... 
]

我想要的是只获取一个类似这样的数组:

[55987, 6717024, 6717024, ...]

我可以遍历数组并复制我想要的值,但我不确定这是否是最好的方法。因此,我想知道是否有可能删除数组的列,或者是否可以创建带有lambda函数或其他函数的切片。

谢谢。

英文:

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\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...

Thank you

答案1

得分: 0

这更多是一个正则表达式引擎的问题,因为引擎将以以下格式输出结果:

res[0] // 将是第一个匹配的“整个字符串”出现
res[0][0] // 将是整个字符串的匹配
res[0][1] // 将是第一个分组匹配 -- 括号中的内容
res[0]['some_name'] // 将是一个命名分组匹配

你需要做的是迭代res[i]并检索res[i][1]

由于你尝试匹配的正则表达式可能非常复杂 -- 可能有许多分组匹配、许多命名分组匹配等 -- 结果变量也可能相当复杂。

由于结果变量的(可能)复杂性,正则表达式库没有必要为你提供完全符合你所描述需求的函数,因为这些函数的用途非常有限。

此外,编写这样的代码片段或函数是一个简单的任务,所以你必须根据你非常特定的需求自己混合和匹配。

英文:

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.

huangapple
  • 本文由 发表于 2013年12月11日 04:03:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/20504118.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定