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
Simple function with return value in Golang
How to add Watermark or Merge two image?
How to create thumbnail of an image?
How to check if a string contains a white space in Golang?
How to write backslash in Golang string?
Golang Convert String into Snake Case
How pointer & and * and ** works in Golang?
Dereferencing a pointer from another package
How to use a mutex to define critical sections of code and fix race conditions?
Golang import package inside package