Started working on '-m num' flag : print the <num>th match
This commit is contained in:
12
main.go
12
main.go
@@ -606,6 +606,7 @@ func main() {
|
|||||||
multiLineFlag := flag.Bool("t", false, "Multi-line mode. Treats newline just like any character.")
|
multiLineFlag := flag.Bool("t", false, "Multi-line mode. Treats newline just like any character.")
|
||||||
printMatchesFlag := flag.Bool("p", false, "Prints start and end index of each match. Can only be used with '-t' for multi-line mode.")
|
printMatchesFlag := flag.Bool("p", false, "Prints start and end index of each match. Can only be used with '-t' for multi-line mode.")
|
||||||
caseInsensitiveFlag = flag.Bool("i", false, "Case-insensitive. Disregard the case of all characters.")
|
caseInsensitiveFlag = flag.Bool("i", false, "Case-insensitive. Disregard the case of all characters.")
|
||||||
|
matchNum := flag.Int("m", 0, "Print the match with the given index. Eg. -m 3 prints the third match.")
|
||||||
substituteText := flag.String("s", "", "Substitute the contents of each match with the given string. Overrides -o and -v")
|
substituteText := flag.String("s", "", "Substitute the contents of each match with the given string. Overrides -o and -v")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
@@ -619,14 +620,23 @@ func main() {
|
|||||||
if *onlyFlag {
|
if *onlyFlag {
|
||||||
*lineFlag = false
|
*lineFlag = false
|
||||||
}
|
}
|
||||||
// Check if substitute text has been enabled
|
// Check if substitute and matchNum flags have been enabled
|
||||||
substituteFlagEnabled := false
|
substituteFlagEnabled := false
|
||||||
|
matchNumFlagEnabled := false
|
||||||
flag.Visit(func(f *flag.Flag) {
|
flag.Visit(func(f *flag.Flag) {
|
||||||
if f.Name == "s" {
|
if f.Name == "s" {
|
||||||
substituteFlagEnabled = true
|
substituteFlagEnabled = true
|
||||||
}
|
}
|
||||||
|
if f.Name == "m" {
|
||||||
|
matchNumFlagEnabled = true
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// Validate matchNumFlag - must be positive integer
|
||||||
|
if matchNumFlagEnabled && *matchNum < 1 {
|
||||||
|
panic("Invalid match number to print.")
|
||||||
|
}
|
||||||
|
|
||||||
// Process:
|
// Process:
|
||||||
// 1. Convert regex into postfix notation (Shunting-Yard algorithm)
|
// 1. Convert regex into postfix notation (Shunting-Yard algorithm)
|
||||||
// a. Add explicit concatenation operators to facilitate this
|
// a. Add explicit concatenation operators to facilitate this
|
||||||
|
Reference in New Issue
Block a user