(target, regexStr ref.Val)
| 264 | } |
| 265 | |
| 266 | func 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 | |
| 295 | func extractAll(target, regexStr ref.Val) ref.Val { |
| 296 | t := string(target.(types.String)) |
nothing calls this directly
no test coverage detected