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

Function extract

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

Source from the content-addressed store, hash-verified

264}
265
266func extract(target, regexStr ref.Val) ref.Val {
267 t := string(target.(types.String))
268 r := string(regexStr.(types.String))
269 re, err := regexp.Compile(r)
270 if err != nil {
271 return types.WrapErr(err)
272 }
273
274 if len(re.SubexpNames())-1 > 1 {
275 return types.WrapErr(fmt.Errorf("regular expression has more than one capturing group: %q", r))
276 }
277
278 matches := re.FindStringSubmatch(t)
279 if len(matches) == 0 {
280 return types.OptionalNone
281 }
282
283 // If there is a capturing group, return the first match; otherwise, return the whole match.
284 if len(matches) > 1 {
285 capturedGroup := matches[1]
286 // If optional group is empty, return OptionalNone.
287 if capturedGroup == "" {
288 return types.OptionalNone
289 }
290 return types.OptionalOf(types.String(capturedGroup))
291 }
292 return types.OptionalOf(types.String(matches[0]))
293}
294
295func extractAll(target, regexStr ref.Val) ref.Val {
296 t := string(target.(types.String))

Callers

nothing calls this directly

Calls 4

WrapErrFunction · 0.92
OptionalOfFunction · 0.92
StringTypeAlias · 0.92
CompileMethod · 0.65

Tested by

no test coverage detected