Search code examples
logstash

Replace regex pattern in array of strings - Logstash


I'm trying to remove url prefix from urls Array in logstash using ruby: The url looks like this: urlArray = ['https://www.google.com','https://www.bcc.com'] I tried to use other array and do something like:

urlArray.each{ |url| newUrlArray.push(url.gsub("(https?://)?(www\.)?","")) }

I also tried:

newUrlArray = urlArray.map{ |url| url.gsub("(https?://)?(www\.)?","") }

I think I miss here something with the gsub.

Thanks


Solution

  • I suggest to use slice and then capture the group you're interested in.

    urlArray = [
      "https://www.google.com", 
      "https://www.bcc.com", 
      "http://hello.com", 
      "www.world.com"
    ]
    newUrlArray = []
    
    pattern = /(https?:\/\/)?(www.)?(.*)/
    
    urlArray.each{ |url| newUrlArray.push(url.slice(pattern, 3)) }
    
    puts newUrlArray
    # google.com
    # bcc.com
    # hello.com
    # world.com