Repository navigation
Use ECMA 262 definition of $ (end of string like \Z) - #34
Merged
Merged
Conversation
We often set patterns on strings e.g.
{"type": "string", "pattern": "^[abc]+$"}
This matches e.g. `aaa`, `b`.
It also surprisingly also matches these strings with a final \n e.g.
`aaa\n`, `b\n`
The python $ character matches end of string. But it also matches if a \n
is the last character even when multiline is False (default).
Instead in python re \Z matches the end of the string only. This is what
we want but it is very python specific and so wouldn't be friendly in
documentation.
In this commit, $ is swapped with \Z inside the pattern handling code.
A breaking change with my substitution here is that if you have used $
inside a set of characters [$] you now would need to escape it like
[\$].
Python docs:
"Matches the end of the string or just before the newline at the end of the string, and in MULTILINE mode also matches before a newline. foo matches both ‘foo’ and ‘foobar’, while the regular expression foo$ matches only ‘foo’. More interestingly, searching for foo.$ in 'foo1\nfoo2\n' matches ‘foo2’ normally, but ‘foo1’ in MULTILINE mode; searching for a single $ in 'foo\n' will find two (empty) matches: one just before the newline, and one at the end of the string."
https://docs.python.org/3/library/re.html
The json schema draft 3 states:
"Regular expressions SHOULD follow the regular expression specification from ECMA 262/Perl 5"
https://tools.ietf.org/html/draft-zyp-json-schema-03
In that regular expression definition:
If multiline is FALSE, $ must match the end of the string.
If multiline is TRUE, $ may match \n.
https://www.ecma-international.org/ecma-262/5.1/#sec-15.10.2.6
The python $ allows a \n as the final character. The email address `abc@def.com\n` should fail validation. Instead of $ we actually want \Z.
Owner
|
Very interesting. I didn't know about this |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
We often set patterns on strings e.g.
{"type": "string", "pattern": "^[abc]+$"}This matches e.g.
aaa,b.It also surprisingly matches these strings with a final \n e.g.
aaa\n,b\nThe python
$character matches end of string. But it also matches if a \nis the last character even when multiline is False (default).
Instead in python re
\Zmatches the end of the string only. This is whatwe want but it is very python specific and so wouldn't be friendly in
documentation.
In this commit,
$is swapped with\Zinside the pattern handling code.A breaking change with my substitution here is that if you have used
$inside a set of characters
[$]you now would need to escape it like[\$].Python docs:
"$: Matches the end of the string or just before the newline at the end of the string, and in MULTILINE mode also matches before a newline. foo matches both ‘foo’ and ‘foobar’, while the regular expression foo$ matches only ‘foo’. More interestingly, searching for foo.$ in 'foo1\nfoo2\n' matches ‘foo2’ normally, but ‘foo1’ in MULTILINE mode; searching for a single $ in 'foo\n' will find two (empty) matches: one just before the newline, and one at the end of the string."
https://docs.python.org/3/library/re.html
The json schema draft 3 states:
"Regular expressions SHOULD follow the regular expression specification from ECMA 262/Perl 5"
https://tools.ietf.org/html/draft-zyp-json-schema-03
In that regular expression definition:
If multiline is FALSE, $ must match the end of the string.
If multiline is TRUE, $ may match \n.
https://www.ecma-international.org/ecma-262/5.1/#sec-15.10.2.6