Skip to content

Commit

Permalink
Fix incorrect regex on Person.name field (#1089)
Browse files Browse the repository at this point in the history
The example regular expression introduced in
10eb9db
does not seem to work as intended, due to nesting a negated range inside
another range:

```
❯ jshell --class-path ~/.m2/repository/com/google/re2j/re2j/1.7/re2j-1.7.jar
|  Welcome to JShell -- Version 17.0.9
|  For an introduction type: /help intro

jshell> com.google.re2j.Pattern p = com.google.re2j.Pattern.compile("^[^[0-9]A-Za-z]+( [^[0-9]A-Za-z]+)*$");
p ==> ^[^[0-9]A-Za-z]+( [^[0-9]A-Za-z]+)*$

jshell> p.matches(pattern, "Protocol Buffers");
$4 ==> false

jshell> p.matches(pattern, "Matt Brown");
$5 ==> false
```

The nesting of ranges in the example is also redundant - if you are
going to specify that only `[A-Za-z]` is valid than including [^0-9]` in
that range is not necessary.

This commit fixes the example regular expression to match the example
"name" mentioned later in the README.

---------

Co-authored-by: Chris Roche <[email protected]>
  • Loading branch information
mattnworb and rodaine authored Mar 25, 2024
1 parent abd77e9 commit 1157c05
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 3 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ message Person {
string email = 2 [(validate.rules).string.email = true];
string name = 3 [(validate.rules).string = {
pattern: "^[^[0-9]A-Za-z]+( [^[0-9]A-Za-z]+)*$",
pattern: "^[A-Za-z]+( [A-Za-z]+)*$",
max_bytes: 256,
}];
Expand All @@ -58,7 +58,7 @@ p.Id = 1000
err = p.Validate() // err: Email must be a valid email address
p.Email = "[email protected]"

err = p.Validate() // err: Name must match pattern '^[^\d\s]+( [^\d\s]+)*$'
err = p.Validate() // err: Name must match pattern '^[A-Za-z]+( [A-Za-z]+)*$'
p.Name = "Protocol Buffer"

err = p.Validate() // err: Home is required
Expand Down
2 changes: 1 addition & 1 deletion python/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ except ValidationFailed as err:
print(err)
# p.id is not greater than 999
# p.email is not a valid email
# p.name pattern does not match ^[^[0-9]A-Za-z]+( [^[0-9]A-Za-z]+)*$
# p.name pattern does not match ^[A-Za-z]+( [A-Za-z]+)*$
# home is required.
```

Expand Down

0 comments on commit 1157c05

Please sign in to comment.