Skip to content

Commit

Permalink
remove moreThanRegex start match
Browse files Browse the repository at this point in the history
  • Loading branch information
jpinsonneau committed Aug 2, 2023
1 parent 7c0dbe8 commit 01c1c48
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
4 changes: 2 additions & 2 deletions pkg/loki/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ func notContainsKeyLineFilter(key string) lineFilter {
func moreThanRegex(sb *strings.Builder, value string) {
// match each number greater than specified value using regex
// example for 123:
// ^ ( 12[3-9] | 1[3-9][0-9]+ | [2-9][0-9]+ | [1-9][0-9]{3,} )
// ( 12[3-9] | 1[3-9][0-9]+ | [2-9][0-9]+ | [1-9][0-9]{3,} )
// | | | |
// | | | ↪ match number more than 1000
// | | |
Expand All @@ -204,7 +204,7 @@ func moreThanRegex(sb *strings.Builder, value string) {
// |
// ↪ match any number from 123 to 129

sb.WriteString("^(")
sb.WriteString("(")
for i, r := range value {
if i < len(value)-1 {
sb.WriteRune(r)
Expand Down
18 changes: 9 additions & 9 deletions pkg/loki/filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ func TestWriteInto_1(t *testing.T) {
sb := strings.Builder{}
moreThanRegex(&sb, "1")
result := sb.String()
assert.Equal(t, result, "^([1-9]|[1-9][0-9]{1,})")
assert.Equal(t, result, "([1-9]|[1-9][0-9]{1,})")
reg := regexp.MustCompile(result)
fmt.Printf("\nRegex: %s", result)
for _, i := range []int{-1, 0, 1, 2, 3, 10, 100} {
for _, i := range []int{0, 1, 2, 3, 10, 100} {
val := strconv.Itoa(i)
assert.Equal(t, reg.MatchString(val), i >= 1, fmt.Sprintf("Value: %d", i))
}
Expand All @@ -27,10 +27,10 @@ func TestWriteInto_7(t *testing.T) {
sb := strings.Builder{}
moreThanRegex(&sb, "7")
result := sb.String()
assert.Equal(t, result, "^([7-9]|[1-9][0-9]{1,})")
assert.Equal(t, result, "([7-9]|[1-9][0-9]{1,})")
reg := regexp.MustCompile(result)
fmt.Printf("\nRegex: %s", result)
for _, i := range []int{-1, 0, 1, 3, 7, 10, 11, 100} {
for _, i := range []int{0, 1, 3, 7, 10, 11, 100} {
val := strconv.Itoa(i)
assert.Equal(t, reg.MatchString(val), i >= 7, fmt.Sprintf("Value: %d", i))
}
Expand All @@ -40,10 +40,10 @@ func TestWriteInto_15(t *testing.T) {
sb := strings.Builder{}
moreThanRegex(&sb, "15")
result := sb.String()
assert.Equal(t, result, "^(1[5-9]|[2-9][0-9]|[1-9][0-9]{2,})")
assert.Equal(t, result, "(1[5-9]|[2-9][0-9]|[1-9][0-9]{2,})")
reg := regexp.MustCompile(result)
fmt.Printf("\nRegex: %s", result)
for _, i := range []int{-1, 0, 1, 5, 14, 15, 16, 150, 1050} {
for _, i := range []int{0, 1, 5, 14, 15, 16, 150, 1050} {
val := strconv.Itoa(i)
assert.Equal(t, reg.MatchString(val), i >= 15, fmt.Sprintf("Value: %d", i))
}
Expand All @@ -53,10 +53,10 @@ func TestWriteInto_123(t *testing.T) {
sb := strings.Builder{}
moreThanRegex(&sb, "123")
result := sb.String()
assert.Equal(t, result, "^(12[3-9]|1[3-9][0-9]|[2-9][0-9]{2,}|[1-9][0-9]{3,})")
assert.Equal(t, result, "(12[3-9]|1[3-9][0-9]|[2-9][0-9]{2,}|[1-9][0-9]{3,})")
reg := regexp.MustCompile(result)
fmt.Printf("\nRegex: %s", result)
for _, i := range []int{-123, 0, 1, 10, 100, 115, 123, 124, 150, 200, 1230} {
for _, i := range []int{0, 1, 10, 100, 115, 123, 124, 150, 200, 1230} {
val := strconv.Itoa(i)
assert.Equal(t, reg.MatchString(val), i >= 123, fmt.Sprintf("Value: %d", i))
}
Expand All @@ -66,7 +66,7 @@ func TestWriteInto_7654(t *testing.T) {
sb := strings.Builder{}
moreThanRegex(&sb, "7654")
result := sb.String()
assert.Equal(t, result, "^(765[4-9]|76[6-9][0-9]|7[7-9][0-9]{2,}|[8-9][0-9]{3,}|[1-9][0-9]{4,})")
assert.Equal(t, result, "(765[4-9]|76[6-9][0-9]|7[7-9][0-9]{2,}|[8-9][0-9]{3,}|[1-9][0-9]{4,})")
reg := regexp.MustCompile(result)
fmt.Printf("\nRegex: %s", result)
for _, i := range []int{0, 1, 1000, 7654, 7655, 10000} {
Expand Down

0 comments on commit 01c1c48

Please sign in to comment.