Go (RE2)

Relative Path in GO

Matches relative file paths (./, ../, or path/to/file).

Try it in the GO tester →

Pattern

regexGO
^(\.{1,2}\/)?([^\/\0]+\/)*[^\/\0]+$

Go (RE2) code

goGo
package main

import (
	"fmt"
	"regexp"
)

func main() {
	re := regexp.MustCompile(`^(\.{1,2}\/)?([^\/\0]+\/)*[^\/\0]+$`)
	input := `./src/index.ts`
	for _, match := range re.FindAllString(input, -1) {
		fmt.Println(match)
	}
}

Uses `regexp.MustCompile` (panics on bad patterns at startup) and `FindAllString` for all matches.

How the pattern works

`^(\.{1,2}\/)?` optionally matches `./` or `../` prefix. `([^\/\0]+\/)*` matches zero or more directory segments. `[^\/\0]+$` matches the final file segment.

Examples

Input

./src/index.ts

Matches

  • ./src/index.ts

Input

../parent/file.js

Matches

  • ../parent/file.js

Input

file.txt

Matches

  • file.txt

Same pattern, other engines

← Back to Relative Path overview (all engines)