How to implement Regex inside AWS Wafv2 IP set from IP white list txt file?


I have been trying to convert an IP whitelist rule from AWS WAF Classic to WAFv2, and struggling to finish it.

I have to Regex into 3 txt files grabbing IPV4 & IPV6 addresses.
Can’t seem to get the loop to work, so it pulls the IP addresses & load it into my TF resource.

How do I loop through this correctly? I’m trying to filter through the file with Regex to take out each IP address and insert it into my IP resource.

Staring with IPV4, will clone it for IPV6.

     resource "aws_wafv2_ip_set" "example" {
      name               = "example"
      description        = "Example IP set"
      scope              = var.target_scope
      ip_address_version = "IPV4"
for_each = var.ipv4_whitelist
      addresses          = each.key
      tags = {
        Tag1 = "Value1"
        Tag2 = "Value2"

    data "template_file" "ip_whitelist" {
      template = file("./ip_whitelist.txt")

    locals {

      ipv_4 = { 
        {for ipv4 in data.template.ip_whitelist }
      if contains length("\\d{0,3}\\.\\d{0,3}\\.\\d{0,3}\\.\\d{0,3}\\/\\d{0,2}", var.whitelist_ip))

    variable ipv4_whitelist
    default = []


Thanks in advance.