Skip to content
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

Partial matching a string against a regex #165

Open
sassanh opened this issue Mar 3, 2017 · 5 comments
Open

Partial matching a string against a regex #165

sassanh opened this issue Mar 3, 2017 · 5 comments

Comments

@sassanh
Copy link

sassanh commented Mar 3, 2017

Suppose that I have this regular expression: /abcd/ Suppose that I want to check the user input against that regex and disallow entering invalid characters in the input. When user inputs "ab", it fails as an match for the regex, but I can't disallow entering "a" and then "b" as user can't enter all 4 characters at once (except for copy/paste). So what I need here is a partial match which checks if an incomplete string can be potentially a match for a regex.

Java has something for this purpose: .hitEnd() (described here http://glaforge.appspot.com/article/incomplete-string-regex-matching) python doesn't do it natively but has this package that does the job: https://pypi.python.org/pypi/regex.

I didn't find any solution for it in js. It's been asked years ago: http://stackoverflow.com/questions/9060979/javascript-regex-partial-match and even before that: http://stackoverflow.com/questions/416425/check-if-string-is-a-prefix-of-a-javascript-regexp
And I asked it here again: https://stackoverflow.com/questions/42461651/partial-matching-a-string-against-a-regex and still no answer.

Can we have this functionality in xregexp?

@slevithan
Copy link
Owner

slevithan commented Mar 7, 2017

I would love for XRegExp to have this feature but it would be very complex to comprehensively deal with all the edge cases of regex syntax, unless you have access to the regex engine internals or re-implement the whole regex engine in JS. See the discussion in #52.

A more realistic path might be to build support but only for a subset of "easy" regexes that pass a validation precheck. I'd be open to ideas here and pull requests that show how it might work.

@sassanh
Copy link
Author

sassanh commented Mar 7, 2017

I asked same question on stackoverflow, and I got a good temporary answer that maybe we can implement in XRegExp: http://stackoverflow.com/a/42597270/1349278 the complete answer is here:
http://stackoverflow.com/a/41580048/1349278

@sassanh
Copy link
Author

sassanh commented Mar 7, 2017

Another solution would be to check the python implementation. It's complete (even supports back reference): https://bitbucket.org/mrabarnett/mrab-regex/src/4600a157989dc1671e4415ebe57aac53cfda2d8a/regex_3/regex/_regex.c?at=default&fileviewer=file-view-default

@JDMCreator
Copy link

@sassanh, I wrote a code in Javascript to partial match Strings using RegExps : https://github.com/JDMCreator/liveRegExp

@slevithan and you could check it out and see if it's possible to include it inside XRegExp.

@sassanh
Copy link
Author

sassanh commented May 5, 2017

@JDMCreator great, I got a good answer in stackoverflow too: http://stackoverflow.com/a/42597270/1349278 maybe it can be helpful here.

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

No branches or pull requests

3 participants