Print matched content in color
This commit is contained in:
		
							
								
								
									
										9
									
								
								go.mod
									
									
									
									
									
								
							
							
						
						
									
										9
									
								
								go.mod
									
									
									
									
									
								
							@@ -1,3 +1,10 @@
 | 
			
		||||
module re 
 | 
			
		||||
module re
 | 
			
		||||
 | 
			
		||||
go 1.23.1
 | 
			
		||||
 | 
			
		||||
require (
 | 
			
		||||
	github.com/fatih/color v1.18.0 // indirect
 | 
			
		||||
	github.com/mattn/go-colorable v0.1.13 // indirect
 | 
			
		||||
	github.com/mattn/go-isatty v0.0.20 // indirect
 | 
			
		||||
	golang.org/x/sys v0.25.0 // indirect
 | 
			
		||||
)
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										11
									
								
								go.sum
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								go.sum
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,11 @@
 | 
			
		||||
github.com/fatih/color v1.18.0 h1:S8gINlzdQ840/4pfAwic/ZE0djQEH3wM94VfqLTZcOM=
 | 
			
		||||
github.com/fatih/color v1.18.0/go.mod h1:4FelSpRwEGDpQ12mAdzqdOukCy4u8WUtOY6lkT/6HfU=
 | 
			
		||||
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
 | 
			
		||||
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
 | 
			
		||||
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
 | 
			
		||||
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
 | 
			
		||||
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
 | 
			
		||||
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
 | 
			
		||||
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
 | 
			
		||||
golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34=
 | 
			
		||||
golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
 | 
			
		||||
							
								
								
									
										21
									
								
								main.go
									
									
									
									
									
								
							
							
						
						
									
										21
									
								
								main.go
									
									
									
									
									
								
							@@ -4,6 +4,8 @@ import (
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"os"
 | 
			
		||||
	"slices"
 | 
			
		||||
 | 
			
		||||
	"github.com/fatih/color"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
const CONCAT rune = '~'
 | 
			
		||||
@@ -44,7 +46,7 @@ func shuntingYard(re string) string {
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	fmt.Println(string(re_postfix))
 | 
			
		||||
	// fmt.Println(string(re_postfix))
 | 
			
		||||
 | 
			
		||||
	opStack := make([]rune, 0)  // Operator stack
 | 
			
		||||
	outQueue := make([]rune, 0) // Output queue
 | 
			
		||||
@@ -165,12 +167,19 @@ func main() {
 | 
			
		||||
	var re string
 | 
			
		||||
	re = os.Args[1]
 | 
			
		||||
	re_postfix := shuntingYard(re)
 | 
			
		||||
	fmt.Println(re_postfix)
 | 
			
		||||
	start := thompson(re_postfix)
 | 
			
		||||
	s, e, matched := match(start, os.Args[2])
 | 
			
		||||
	// fmt.Println(re_postfix)
 | 
			
		||||
	startState := thompson(re_postfix)
 | 
			
		||||
	start, end, matched := match(startState, os.Args[2])
 | 
			
		||||
	if matched {
 | 
			
		||||
		fmt.Printf("Matched from %d to %d\n", s, e)
 | 
			
		||||
		for i, c := range os.Args[2] {
 | 
			
		||||
			if i >= start && i < end {
 | 
			
		||||
				color.New(color.FgRed).Printf("%c", c)
 | 
			
		||||
			} else {
 | 
			
		||||
				fmt.Printf("%c", c)
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
		fmt.Printf("\n")
 | 
			
		||||
	} else {
 | 
			
		||||
		fmt.Printf("No match found.\n")
 | 
			
		||||
		fmt.Println(os.Args[2])
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user