Hello,
it would be great if there could be some built-in way to check if a regex matches a string in full.
This would correspond to the fullmatch function in Python: https://docs.python.org/3/library/re.html
Rationale:
In my use case, the regex string is provided as user input and then compiled. The compiled regex is then used to match against some string, and the match is only considered successful if that string is matched completely. For convenience reasons, the user is not expected to enclose their regex with "^" and "$" anchors.
I have tried to emulate this "fullmatch" with the current feature set and the tdfa engine.
What I have come up with are the following options:
Option A: Use the (MatchOffset, MatchLength) overload of the match function. If a full match is possible, the tdfa engine will find it, and then MatchLength will be equal to the length of the string that was matched. This works, but it brings some performance disadvantages: The lack of the anchors seems to slow down the matching significantly (see below). Moreover, the MatchLength result is not really needed in my case, its only purpose is to be compared against the length of the string being matched. Moreover, this string length is also only used for the comparison against MatchLength.
Option B: Enclose the regex string between "^" and "$" anchors and use the Bool overload of the match function.
This is much faster than option A, about a factor of 10. The problem is that this does not work reliably. It does not work if the user has added the "^" and "$" anchors themselves. Figuring this out, however, is tricky, particularly for the "$": A "$" at the end of the user provided regex string could be a quoted "$". Moreover, there are corner cases where adding the anchors also does not work as intended: a stray "\" at the end of a regex string, which would lead to a compile error of the regex engine, would with the added "$" become a valid regex, but the "$" would then be treated as a character rather than an anchor...
Maybe I have missed some option C? If so, I would be glad to learn about it.
If not, then I hope I could convince you why this feature would add value to the regex library.
In any case, thanks a lot for this great piece of software and its nicely designed user interface.
Kind regards,
Dirk
Hello,
it would be great if there could be some built-in way to check if a regex matches a string in full.
This would correspond to the fullmatch function in Python: https://docs.python.org/3/library/re.html
Rationale:
In my use case, the regex string is provided as user input and then compiled. The compiled regex is then used to match against some string, and the match is only considered successful if that string is matched completely. For convenience reasons, the user is not expected to enclose their regex with "^" and "$" anchors.
I have tried to emulate this "fullmatch" with the current feature set and the tdfa engine.
What I have come up with are the following options:
Option A: Use the (MatchOffset, MatchLength) overload of the match function. If a full match is possible, the tdfa engine will find it, and then MatchLength will be equal to the length of the string that was matched. This works, but it brings some performance disadvantages: The lack of the anchors seems to slow down the matching significantly (see below). Moreover, the MatchLength result is not really needed in my case, its only purpose is to be compared against the length of the string being matched. Moreover, this string length is also only used for the comparison against MatchLength.
Option B: Enclose the regex string between "^" and "$" anchors and use the Bool overload of the match function.
This is much faster than option A, about a factor of 10. The problem is that this does not work reliably. It does not work if the user has added the "^" and "$" anchors themselves. Figuring this out, however, is tricky, particularly for the "$": A "$" at the end of the user provided regex string could be a quoted "$". Moreover, there are corner cases where adding the anchors also does not work as intended: a stray "\" at the end of a regex string, which would lead to a compile error of the regex engine, would with the added "$" become a valid regex, but the "$" would then be treated as a character rather than an anchor...
Maybe I have missed some option C? If so, I would be glad to learn about it.
If not, then I hope I could convince you why this feature would add value to the regex library.
In any case, thanks a lot for this great piece of software and its nicely designed user interface.
Kind regards,
Dirk