How to declare a RegEx with character repetition to match the strings "http", "htttp", "httttp", etc in Go

1 Answer

0 votes
package main

import (
    "fmt"
    "regexp"
)

func main() {
    // Declare the regex pattern
    pattern := regexp.MustCompile(`^htt+p$`)

    // Test strings
    testStrings := []string{"http", "htttp", "httttp", "httpp", "htp"}

    // Check matches
    for _, test := range testStrings {
        match := pattern.MatchString(test)
        fmt.Printf("Matches \"%s\": %t\n", test, match)
    }
}



// Matches "httpp": True or false, depending on how matches() method works
 
 
/*
run:

Matches "http": true
Matches "htttp": true
Matches "httttp": true
Matches "httpp": false
Matches "htp": false
 
*/

 



answered May 16, 2025 by avibootz
...