Can re.fullmatch() eliminate the need for string anchors in regex
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
add a comment |
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
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
add a comment |
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
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
python regex
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
add a comment |
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
add a comment |
1 Answer
1
active
oldest
votes
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.
Thank you sir. This is what I was looking for.
– Chris
Jun 18 '16 at 17:49
2
Tim Peters observesre.match(r'a|ab$', 'ab').group()
returns'a'
, whilere.fullmatch(r'a|ab', 'ab').group()
returns'ab'
. Sore.fullmatch(...)
is not simply a replacement forre.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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
Thank you sir. This is what I was looking for.
– Chris
Jun 18 '16 at 17:49
2
Tim Peters observesre.match(r'a|ab$', 'ab').group()
returns'a'
, whilere.fullmatch(r'a|ab', 'ab').group()
returns'ab'
. Sore.fullmatch(...)
is not simply a replacement forre.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
add a comment |
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.
Thank you sir. This is what I was looking for.
– Chris
Jun 18 '16 at 17:49
2
Tim Peters observesre.match(r'a|ab$', 'ab').group()
returns'a'
, whilere.fullmatch(r'a|ab', 'ab').group()
returns'ab'
. Sore.fullmatch(...)
is not simply a replacement forre.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
add a comment |
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.
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.
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 observesre.match(r'a|ab$', 'ab').group()
returns'a'
, whilere.fullmatch(r'a|ab', 'ab').group()
returns'ab'
. Sore.fullmatch(...)
is not simply a replacement forre.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
add a comment |
Thank you sir. This is what I was looking for.
– Chris
Jun 18 '16 at 17:49
2
Tim Peters observesre.match(r'a|ab$', 'ab').group()
returns'a'
, whilere.fullmatch(r'a|ab', 'ab').group()
returns'ab'
. Sore.fullmatch(...)
is not simply a replacement forre.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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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