Regular expression to extract filename from given path in Golang
Example
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`^(.*/)?(?:$|(.+?)(?:(\.[^.]*$)|$))`)
str1 := `http://www.golangprograms.com/regular-expressions.html`
match1 := re.FindStringSubmatch(str1)
fmt.Println(match1[2])
str2 := `/home/me/dir3/dir3a/dir3ac/filepat.png`
match2 := re.FindStringSubmatch(str2)
fmt.Println(match2[2])
}
Output
regular-expressions
filepat
Most Helpful This Week
Regular expression to extract text between square brackets
How to replace emoji characters in string using regex in Golang?
Regular expression to extract DNS host-name or IP Address from string
Regular expression to extract domain from URL
Regular expression to validate common Credit Card Numbers
Regular expression to validate phone number