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
How to use Ellipsis (...) in Golang?
How to delete or remove element from a Map?
How to reads and decodes JSON values from an input stream?
How to create Map using the make function in Go?
Example Function that takes an interface type as value and pointer?
How To Make HTTP Requests in Go?
How to concatenate two or more slices in Golang?
Golang Read Write Create and Delete text file
User Defined Function Types in Golang
How to create Empty and Nil Slice?