Skip to content

Commit

Permalink
added match value extraction
Browse files Browse the repository at this point in the history
  • Loading branch information
rhaeguard committed Sep 20, 2023
1 parent 4856441 commit e2716be
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 6 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ a very simple regex engine written in go.
- [x] `( )` capturing group or subexpression
- [x] `\n` backreference, e.g, `(dog)\1` where `n` is in `[0, 9]`
- [x] `\k<name>` named backreference, e.g, `(?<animal>dog)\k<animal>`
- [x] extracting the string that matches with the regex
- [x] `\` escape character
- [x] support special characters - context dependant
- [ ] better error handling in the API
Expand Down
6 changes: 0 additions & 6 deletions check.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,7 @@ func (s *State) check(inputString string, pos int, started bool, ctx *regexCheck
ctx.groups[groupName] = c
}
}
}
}

if s.groups != nil {
// if this state has groups associated with it
// go through each group
for _, capturedGroup := range s.groups {
// if the group ends
if capturedGroup.end {
// for each name of the captured group
Expand Down
34 changes: 34 additions & 0 deletions lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,40 @@ func (s *State) Test(inputString string) Result {
}
}

func (s *State) FindMatches(inputString string) []Result {
var results []Result
start := -1
for start < len(inputString) {
checkContext := &regexCheckContext{
groups: map[string]*capture{},
}
result := s.check(inputString, start, s.startOfText, checkContext)
if !result {
break
}
// prepare the result
groups := map[string]string{}

if result {
// extract strings from the groups
for groupName, captured := range checkContext.groups {
groups[groupName] = captured.string(inputString)
if groupName == "0" {
start = captured.end + 1
}
}
}

r := Result{
matches: result,
groups: groups,
}

results = append(results, r)
}
return results
}

// Check compiles the regexString and tests the inputString against it
func Check(regexString string, inputString string) Result {
return Compile(regexString).Test(inputString)
Expand Down
29 changes: 29 additions & 0 deletions lib_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,35 @@ func TestCheck(t *testing.T) {
}
}

func TestFindMatches(t *testing.T) {
var data = []struct {
regexString, input string
expected []map[string]string
}{
{`[0-9]{3}-[0-9]{3}-[0-9]{2}-[0-9]{2}`, `hi 123-678-99-32 is my number, so is 239-987-63-21.`, []map[string]string{
{"0": "123-678-99-32"},
{"0": "239-987-63-21"},
}},
}

for _, test := range data {
testName := fmt.Sprintf("%s-%s-%v", test.regexString, test.input, test.expected)
t.Run(testName, func(t *testing.T) {
results := Compile(test.regexString).FindMatches(test.input)
if len(results) != len(test.expected) {
t.Fail()
}
for i, expected := range test.expected {
for k, v := range expected {
if results[i].groups[k] != v {
t.Fail()
}
}
}
})
}
}

func TestCheckForDev(t *testing.T) {
var data = []struct {
regexString, input string
Expand Down

0 comments on commit e2716be

Please sign in to comment.