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
Get Year, Month, Day, Hour, Min and Second from a specified date
How to get current IP form ipify.org ?
How to concatenate two or more slices in Golang?
Golang String Concatenation
What is Rune? How to get ASCII value of any character in Go?
Simple function with return value in Golang
How to convert Boolean Type to String in Go?
Regular expression to validate email address
Runtime package variables
Dynamic JSON parser without Struct in Golang