New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Regex.split with submatches #803

Closed
spikefoo opened this Issue Jan 6, 2017 · 1 comment

Comments

Projects
None yet
2 participants
@spikefoo

spikefoo commented Jan 6, 2017

(Regex meta issue: #722)

What is the intended behavior of Regex.split when using a seperator regex with subpatterns?

Regex with no subpatterns

If you use Regex.split with a seperator regex that has no subpatterns, the result does not include the seperators. E.g.:

elm-repl> split All (regex "&|#") "A&B#C&D"
["A","B","C","D"] : List String

The implementation of Regex.split uses Javascript String#split:

node> "A&B#C&D".split(/&|#/)
[ 'A', 'B', 'C', 'D' ]

Regex with subpatterns

The documentation for Regex.split doesn't specify the intended behavior for a regex with subpatterns (and there are no tests for this case), but it's still implemented using Javascript String#split. This means that subpatterns are included in the resulting array:

elm-repl> split All (regex "(&|#)") "A&B#C&D"
["A","&","B","#","C","&","D"] : List String

This is a nice feature to have, and the docs and tests can be updated to support it. But it breaks when using optional subpatterns:

Regex with optional subpatterns

Using optional subpatterns in Javascript can cause the result to have undefined elements:

node> "A&B#C&D".split(/(&)|(#)/)
[ 'A', '&', undefined, 'B', undefined, '#', 'C', '&', undefined, 'D' ]

This breaks Elm:

elm-repl> split All (regex "(&)|(#)") "A&B#C&D"
["A","&",<internal structure>,"B",<internal structure>,"#","C","&",<internal structure>,"D"] : List String

It could be fixed by returning List (Maybe String) instead of List String, but that would complicate the use of Regex.split in the common case where subpatterns aren't used.

Ideally, it should be easy to use Regex.split in the first case (no subpatterns) and preferably the second case too (no optional subpatterns), while keeping Regex.split safe.

@process-bot

This comment has been minimized.

Show comment
Hide comment
@process-bot

process-bot Jan 6, 2017

Thanks for the issue! Make sure it satisfies this checklist. My human colleagues will appreciate it!

Here is what to expect next, and if anyone wants to comment, keep these things in mind.

process-bot commented Jan 6, 2017

Thanks for the issue! Make sure it satisfies this checklist. My human colleagues will appreciate it!

Here is what to expect next, and if anyone wants to comment, keep these things in mind.

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