Can re.fullmatch() eliminate the need for string anchors in regex












1















Consider the following regex, which checks for password strength. It has the start and end string anchors, to ensure it's matching the entire string.



pattern = re.compile(r'^(?=.*[A-Z])(?=.*[a-z])(?=.*d)(?=.*[$@$!%*#?&.])[A-Za-zd$@$!%*#?&.]{8,}$')
while True:
user_pass = input('Enter a secure password: ')
if re.fullmatch(pattern, user_pass):
print('Successfully changed password')
break
else:
print('Not secure enough. Ensure pass is 8 characters long with at least one upper and lowercase letter, number,'
' and special character.')


I noticed Python 3.5 has a re.fullmatch() which appears to do the same thing, but without the string anchors:



pattern = re.compile(r'(?=.*[A-Z])(?=.*[a-z])(?=.*d)(?=.*[$@$!%*#?&.])[A-Za-zd$@$!%*#?&.]{8,}')
while True:
user_pass = input('Enter a secure password: ')
if re.fullmatch(pattern, user_pass):
print('Successfully changed password')
break
else:
print('Not secure enough. Ensure pass is 8 characters long with at least one upper and lowercase letter, number,'
' and special character.')


Is this the intended purpose of fullmatch? Are there any situations where this could cause unintended issues?










share|improve this question























  • why not just use the anchors, its less extra chars 2 vs 4? that way people who are familiar with regex in general,but maybe not with some python specific stuff, will know what you mean.

    – Joran Beasley
    Jun 18 '16 at 17:45













  • @JoranBeasley That isn't helpful in the least.

    – Chris
    Jun 18 '16 at 17:46











  • you have to type more and its less clear to people who know regex ... im not sure why they even added that ... as such it does not answer your question I know ... thats why its a comment

    – Joran Beasley
    Jun 18 '16 at 17:47













  • @JoranBeasley You're not addressing the question.

    – Chris
    Jun 18 '16 at 17:47











  • The point is that sometimes you have to use an anchor explicitly, e.g. in lookahead conditions. Thus, the title is a bit ambiguous, you can't just forget about using anchors. Only do that when you know what you are doing.

    – Wiktor Stribiżew
    Jun 18 '16 at 19:21
















1















Consider the following regex, which checks for password strength. It has the start and end string anchors, to ensure it's matching the entire string.



pattern = re.compile(r'^(?=.*[A-Z])(?=.*[a-z])(?=.*d)(?=.*[$@$!%*#?&.])[A-Za-zd$@$!%*#?&.]{8,}$')
while True:
user_pass = input('Enter a secure password: ')
if re.fullmatch(pattern, user_pass):
print('Successfully changed password')
break
else:
print('Not secure enough. Ensure pass is 8 characters long with at least one upper and lowercase letter, number,'
' and special character.')


I noticed Python 3.5 has a re.fullmatch() which appears to do the same thing, but without the string anchors:



pattern = re.compile(r'(?=.*[A-Z])(?=.*[a-z])(?=.*d)(?=.*[$@$!%*#?&.])[A-Za-zd$@$!%*#?&.]{8,}')
while True:
user_pass = input('Enter a secure password: ')
if re.fullmatch(pattern, user_pass):
print('Successfully changed password')
break
else:
print('Not secure enough. Ensure pass is 8 characters long with at least one upper and lowercase letter, number,'
' and special character.')


Is this the intended purpose of fullmatch? Are there any situations where this could cause unintended issues?










share|improve this question























  • why not just use the anchors, its less extra chars 2 vs 4? that way people who are familiar with regex in general,but maybe not with some python specific stuff, will know what you mean.

    – Joran Beasley
    Jun 18 '16 at 17:45













  • @JoranBeasley That isn't helpful in the least.

    – Chris
    Jun 18 '16 at 17:46











  • you have to type more and its less clear to people who know regex ... im not sure why they even added that ... as such it does not answer your question I know ... thats why its a comment

    – Joran Beasley
    Jun 18 '16 at 17:47













  • @JoranBeasley You're not addressing the question.

    – Chris
    Jun 18 '16 at 17:47











  • The point is that sometimes you have to use an anchor explicitly, e.g. in lookahead conditions. Thus, the title is a bit ambiguous, you can't just forget about using anchors. Only do that when you know what you are doing.

    – Wiktor Stribiżew
    Jun 18 '16 at 19:21














1












1








1


1






Consider the following regex, which checks for password strength. It has the start and end string anchors, to ensure it's matching the entire string.



pattern = re.compile(r'^(?=.*[A-Z])(?=.*[a-z])(?=.*d)(?=.*[$@$!%*#?&.])[A-Za-zd$@$!%*#?&.]{8,}$')
while True:
user_pass = input('Enter a secure password: ')
if re.fullmatch(pattern, user_pass):
print('Successfully changed password')
break
else:
print('Not secure enough. Ensure pass is 8 characters long with at least one upper and lowercase letter, number,'
' and special character.')


I noticed Python 3.5 has a re.fullmatch() which appears to do the same thing, but without the string anchors:



pattern = re.compile(r'(?=.*[A-Z])(?=.*[a-z])(?=.*d)(?=.*[$@$!%*#?&.])[A-Za-zd$@$!%*#?&.]{8,}')
while True:
user_pass = input('Enter a secure password: ')
if re.fullmatch(pattern, user_pass):
print('Successfully changed password')
break
else:
print('Not secure enough. Ensure pass is 8 characters long with at least one upper and lowercase letter, number,'
' and special character.')


Is this the intended purpose of fullmatch? Are there any situations where this could cause unintended issues?










share|improve this question














Consider the following regex, which checks for password strength. It has the start and end string anchors, to ensure it's matching the entire string.



pattern = re.compile(r'^(?=.*[A-Z])(?=.*[a-z])(?=.*d)(?=.*[$@$!%*#?&.])[A-Za-zd$@$!%*#?&.]{8,}$')
while True:
user_pass = input('Enter a secure password: ')
if re.fullmatch(pattern, user_pass):
print('Successfully changed password')
break
else:
print('Not secure enough. Ensure pass is 8 characters long with at least one upper and lowercase letter, number,'
' and special character.')


I noticed Python 3.5 has a re.fullmatch() which appears to do the same thing, but without the string anchors:



pattern = re.compile(r'(?=.*[A-Z])(?=.*[a-z])(?=.*d)(?=.*[$@$!%*#?&.])[A-Za-zd$@$!%*#?&.]{8,}')
while True:
user_pass = input('Enter a secure password: ')
if re.fullmatch(pattern, user_pass):
print('Successfully changed password')
break
else:
print('Not secure enough. Ensure pass is 8 characters long with at least one upper and lowercase letter, number,'
' and special character.')


Is this the intended purpose of fullmatch? Are there any situations where this could cause unintended issues?







python regex






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jun 18 '16 at 17:42









ChrisChris

883515




883515













  • why not just use the anchors, its less extra chars 2 vs 4? that way people who are familiar with regex in general,but maybe not with some python specific stuff, will know what you mean.

    – Joran Beasley
    Jun 18 '16 at 17:45













  • @JoranBeasley That isn't helpful in the least.

    – Chris
    Jun 18 '16 at 17:46











  • you have to type more and its less clear to people who know regex ... im not sure why they even added that ... as such it does not answer your question I know ... thats why its a comment

    – Joran Beasley
    Jun 18 '16 at 17:47













  • @JoranBeasley You're not addressing the question.

    – Chris
    Jun 18 '16 at 17:47











  • The point is that sometimes you have to use an anchor explicitly, e.g. in lookahead conditions. Thus, the title is a bit ambiguous, you can't just forget about using anchors. Only do that when you know what you are doing.

    – Wiktor Stribiżew
    Jun 18 '16 at 19:21



















  • why not just use the anchors, its less extra chars 2 vs 4? that way people who are familiar with regex in general,but maybe not with some python specific stuff, will know what you mean.

    – Joran Beasley
    Jun 18 '16 at 17:45













  • @JoranBeasley That isn't helpful in the least.

    – Chris
    Jun 18 '16 at 17:46











  • you have to type more and its less clear to people who know regex ... im not sure why they even added that ... as such it does not answer your question I know ... thats why its a comment

    – Joran Beasley
    Jun 18 '16 at 17:47













  • @JoranBeasley You're not addressing the question.

    – Chris
    Jun 18 '16 at 17:47











  • The point is that sometimes you have to use an anchor explicitly, e.g. in lookahead conditions. Thus, the title is a bit ambiguous, you can't just forget about using anchors. Only do that when you know what you are doing.

    – Wiktor Stribiżew
    Jun 18 '16 at 19:21

















why not just use the anchors, its less extra chars 2 vs 4? that way people who are familiar with regex in general,but maybe not with some python specific stuff, will know what you mean.

– Joran Beasley
Jun 18 '16 at 17:45







why not just use the anchors, its less extra chars 2 vs 4? that way people who are familiar with regex in general,but maybe not with some python specific stuff, will know what you mean.

– Joran Beasley
Jun 18 '16 at 17:45















@JoranBeasley That isn't helpful in the least.

– Chris
Jun 18 '16 at 17:46





@JoranBeasley That isn't helpful in the least.

– Chris
Jun 18 '16 at 17:46













you have to type more and its less clear to people who know regex ... im not sure why they even added that ... as such it does not answer your question I know ... thats why its a comment

– Joran Beasley
Jun 18 '16 at 17:47







you have to type more and its less clear to people who know regex ... im not sure why they even added that ... as such it does not answer your question I know ... thats why its a comment

– Joran Beasley
Jun 18 '16 at 17:47















@JoranBeasley You're not addressing the question.

– Chris
Jun 18 '16 at 17:47





@JoranBeasley You're not addressing the question.

– Chris
Jun 18 '16 at 17:47













The point is that sometimes you have to use an anchor explicitly, e.g. in lookahead conditions. Thus, the title is a bit ambiguous, you can't just forget about using anchors. Only do that when you know what you are doing.

– Wiktor Stribiżew
Jun 18 '16 at 19:21





The point is that sometimes you have to use an anchor explicitly, e.g. in lookahead conditions. Thus, the title is a bit ambiguous, you can't just forget about using anchors. Only do that when you know what you are doing.

– Wiktor Stribiżew
Jun 18 '16 at 19:21












1 Answer
1






active

oldest

votes


















3














The fullmatch() function and regex.fullmatch() method are new in Python 3.4.



The changelog is very explicit about it:




This provides a way to be explicit about the goal of the match, which
avoids a class of subtle bugs where $ characters get lost during code
changes or the addition of alternatives to an existing regular
expression.




So, the way you use it is indeed the intended purpose of this feature. It can not lead to unexpected issue, ^ and $ are just carefully added internally.






share|improve this answer


























  • Thank you sir. This is what I was looking for.

    – Chris
    Jun 18 '16 at 17:49






  • 2





    Tim Peters observes re.match(r'a|ab$', 'ab').group() returns 'a', while re.fullmatch(r'a|ab', 'ab').group() returns 'ab'. So re.fullmatch(...) is not simply a replacement for re.match('...$'). That's pretty subtle.

    – unutbu
    Jun 18 '16 at 18:14













  • I see this actually as an example in favor of fullmatch - the original re should probably have been written re.match(r'(a|ab)$', 'ab').group() but the missing parens caused the end-anchoring '$' to be associated only with the 'ab' branch of the alternation.

    – PaulMcG
    Aug 31 '18 at 15:33













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f37899892%2fcan-re-fullmatch-eliminate-the-need-for-string-anchors-in-regex%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









3














The fullmatch() function and regex.fullmatch() method are new in Python 3.4.



The changelog is very explicit about it:




This provides a way to be explicit about the goal of the match, which
avoids a class of subtle bugs where $ characters get lost during code
changes or the addition of alternatives to an existing regular
expression.




So, the way you use it is indeed the intended purpose of this feature. It can not lead to unexpected issue, ^ and $ are just carefully added internally.






share|improve this answer


























  • Thank you sir. This is what I was looking for.

    – Chris
    Jun 18 '16 at 17:49






  • 2





    Tim Peters observes re.match(r'a|ab$', 'ab').group() returns 'a', while re.fullmatch(r'a|ab', 'ab').group() returns 'ab'. So re.fullmatch(...) is not simply a replacement for re.match('...$'). That's pretty subtle.

    – unutbu
    Jun 18 '16 at 18:14













  • I see this actually as an example in favor of fullmatch - the original re should probably have been written re.match(r'(a|ab)$', 'ab').group() but the missing parens caused the end-anchoring '$' to be associated only with the 'ab' branch of the alternation.

    – PaulMcG
    Aug 31 '18 at 15:33


















3














The fullmatch() function and regex.fullmatch() method are new in Python 3.4.



The changelog is very explicit about it:




This provides a way to be explicit about the goal of the match, which
avoids a class of subtle bugs where $ characters get lost during code
changes or the addition of alternatives to an existing regular
expression.




So, the way you use it is indeed the intended purpose of this feature. It can not lead to unexpected issue, ^ and $ are just carefully added internally.






share|improve this answer


























  • Thank you sir. This is what I was looking for.

    – Chris
    Jun 18 '16 at 17:49






  • 2





    Tim Peters observes re.match(r'a|ab$', 'ab').group() returns 'a', while re.fullmatch(r'a|ab', 'ab').group() returns 'ab'. So re.fullmatch(...) is not simply a replacement for re.match('...$'). That's pretty subtle.

    – unutbu
    Jun 18 '16 at 18:14













  • I see this actually as an example in favor of fullmatch - the original re should probably have been written re.match(r'(a|ab)$', 'ab').group() but the missing parens caused the end-anchoring '$' to be associated only with the 'ab' branch of the alternation.

    – PaulMcG
    Aug 31 '18 at 15:33
















3












3








3







The fullmatch() function and regex.fullmatch() method are new in Python 3.4.



The changelog is very explicit about it:




This provides a way to be explicit about the goal of the match, which
avoids a class of subtle bugs where $ characters get lost during code
changes or the addition of alternatives to an existing regular
expression.




So, the way you use it is indeed the intended purpose of this feature. It can not lead to unexpected issue, ^ and $ are just carefully added internally.






share|improve this answer















The fullmatch() function and regex.fullmatch() method are new in Python 3.4.



The changelog is very explicit about it:




This provides a way to be explicit about the goal of the match, which
avoids a class of subtle bugs where $ characters get lost during code
changes or the addition of alternatives to an existing regular
expression.




So, the way you use it is indeed the intended purpose of this feature. It can not lead to unexpected issue, ^ and $ are just carefully added internally.







share|improve this answer














share|improve this answer



share|improve this answer








edited Jun 18 '16 at 17:49

























answered Jun 18 '16 at 17:48









DelganDelgan

9,19854075




9,19854075













  • Thank you sir. This is what I was looking for.

    – Chris
    Jun 18 '16 at 17:49






  • 2





    Tim Peters observes re.match(r'a|ab$', 'ab').group() returns 'a', while re.fullmatch(r'a|ab', 'ab').group() returns 'ab'. So re.fullmatch(...) is not simply a replacement for re.match('...$'). That's pretty subtle.

    – unutbu
    Jun 18 '16 at 18:14













  • I see this actually as an example in favor of fullmatch - the original re should probably have been written re.match(r'(a|ab)$', 'ab').group() but the missing parens caused the end-anchoring '$' to be associated only with the 'ab' branch of the alternation.

    – PaulMcG
    Aug 31 '18 at 15:33





















  • Thank you sir. This is what I was looking for.

    – Chris
    Jun 18 '16 at 17:49






  • 2





    Tim Peters observes re.match(r'a|ab$', 'ab').group() returns 'a', while re.fullmatch(r'a|ab', 'ab').group() returns 'ab'. So re.fullmatch(...) is not simply a replacement for re.match('...$'). That's pretty subtle.

    – unutbu
    Jun 18 '16 at 18:14













  • I see this actually as an example in favor of fullmatch - the original re should probably have been written re.match(r'(a|ab)$', 'ab').group() but the missing parens caused the end-anchoring '$' to be associated only with the 'ab' branch of the alternation.

    – PaulMcG
    Aug 31 '18 at 15:33



















Thank you sir. This is what I was looking for.

– Chris
Jun 18 '16 at 17:49





Thank you sir. This is what I was looking for.

– Chris
Jun 18 '16 at 17:49




2




2





Tim Peters observes re.match(r'a|ab$', 'ab').group() returns 'a', while re.fullmatch(r'a|ab', 'ab').group() returns 'ab'. So re.fullmatch(...) is not simply a replacement for re.match('...$'). That's pretty subtle.

– unutbu
Jun 18 '16 at 18:14







Tim Peters observes re.match(r'a|ab$', 'ab').group() returns 'a', while re.fullmatch(r'a|ab', 'ab').group() returns 'ab'. So re.fullmatch(...) is not simply a replacement for re.match('...$'). That's pretty subtle.

– unutbu
Jun 18 '16 at 18:14















I see this actually as an example in favor of fullmatch - the original re should probably have been written re.match(r'(a|ab)$', 'ab').group() but the missing parens caused the end-anchoring '$' to be associated only with the 'ab' branch of the alternation.

– PaulMcG
Aug 31 '18 at 15:33







I see this actually as an example in favor of fullmatch - the original re should probably have been written re.match(r'(a|ab)$', 'ab').group() but the missing parens caused the end-anchoring '$' to be associated only with the 'ab' branch of the alternation.

– PaulMcG
Aug 31 '18 at 15:33




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f37899892%2fcan-re-fullmatch-eliminate-the-need-for-string-anchors-in-regex%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

SQL update select statement

WPF add header to Image with URL pettitions [duplicate]