Skip to content

Use ECMA 262 definition of $ (end of string like \Z) - #34

Merged
horejsek merged 2 commits into
horejsek:masterfrom
bcaller:regex-dollar
Nov 1, 2018
Merged

horejsek merged 2 commits into
horejsek:masterfrom
bcaller:regex-dollar

Conversation

@bcaller

@bcaller bcaller commented Oct 26, 2018

Copy link
Copy Markdown
Contributor

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\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

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.
@horejsek

horejsek commented Nov 1, 2018

Copy link
Copy Markdown
Owner

Very interesting. I didn't know about this $ vs \Z. Thanks for the pull request!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants