MCPcopy Create free account
hub / github.com/cel-expr/cel-go / extractAll

Function extractAll

ext/regex.go:295–327  ·  view source on GitHub ↗
(target, regexStr ref.Val)

Source from the content-addressed store, hash-verified

293}
294
295func extractAll(target, regexStr ref.Val) ref.Val {
296 t := string(target.(types.String))
297 r := string(regexStr.(types.String))
298 re, err := regexp.Compile(r)
299 if err != nil {
300 return types.WrapErr(err)
301 }
302
303 groupCount := len(re.SubexpNames()) - 1
304 if groupCount > 1 {
305 return types.WrapErr(fmt.Errorf("regular expression has more than one capturing group: %q", r))
306 }
307
308 matches := re.FindAllStringSubmatch(t, -1)
309 result := make([]string, 0, len(matches))
310 if len(matches) == 0 {
311 return types.NewStringList(types.DefaultTypeAdapter, result)
312 }
313
314 if groupCount != 1 {
315 for _, match := range matches {
316 result = append(result, match[0])
317 }
318 return types.NewStringList(types.DefaultTypeAdapter, result)
319 }
320
321 for _, match := range matches {
322 if match[1] != "" {
323 result = append(result, match[1])
324 }
325 }
326 return types.NewStringList(types.DefaultTypeAdapter, result)
327}
328
329func estimateRegexSearchCost(c cost.Estimator, args []cost.AstNode) (cost.SizeEstimate, cost.CostEstimate) {
330 targetSize := cost.EstimateSize(c, args[0])

Callers

nothing calls this directly

Calls 3

WrapErrFunction · 0.92
NewStringListFunction · 0.92
CompileMethod · 0.65

Tested by

no test coverage detected