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 Draw a rectangle in Golang?
How to get Dimensions of an image type jpg jpeg png or gif ?
Functions mess (recursive anonymous function) in Golang
How to check lowercase characters in a string in Golang?
How to write backslash in Golang string?
Get current date and time in various format in golang