Started working on '-m num' flag : print the <num>th match

master
Aadhavan Srinivasan 4 days ago
parent 85eb13287e
commit 13ca954072

@ -606,7 +606,8 @@ 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.")
substituteText := flag.String("s", "", "Substitute the contents of each match with the given string. Overrides -o and -v") 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")
flag.Parse() flag.Parse()
// In multi-line mode, 'dot' metacharacter also matches newline // In multi-line mode, 'dot' metacharacter also matches newline
@ -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

Loading…
Cancel
Save