Regular Expression to find a pangram
Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
Awhile back I encountered a simple programming puzzle to determine whether a string was a pangram, or a perfect pangram. (Although it's worth noting that this particular challenge gave a different meaning than the standard definition of a "perfect pangram," to mean any sentence where every letter appears n times for some positive n). And it was a fun challenge. But recently it got me wondering whether it would be possible to solve this challenge purely with regular expressions. If it is, I expect it will be a pretty gnarly regex. But I'm not even sure it is possible.
Still, if anyone is able to do it, it would be the folks here at Code Golf. So here's the challenge:
Write a regular expression which can determine whether or not a given string is a pangram. You can assume that all non-alpha characters have already been stripped out (or don't, if you really want to challenge yourself), and that everything has been transformed into lowercase.
Bonus challenge: write another regular expression to determine whether the given string is a "perfect pangram" by the definition given above.
Examples of an ordinary pangram:
thequickbrownfoxjumpsoverthelazydog
Example of perfect pangrams:
abcdefghijklmnopqrstuvwxyz
jocknymphswaqfdrugvexblitz
squdgykilpjobzarfnthcwmvexhmfjordwaltzcinqbuskpyxveg
I'm not really sure if this is even possible just with regex alone. If you find that it is, kudos!
regular-expression
add a comment |Â
up vote
1
down vote
favorite
Awhile back I encountered a simple programming puzzle to determine whether a string was a pangram, or a perfect pangram. (Although it's worth noting that this particular challenge gave a different meaning than the standard definition of a "perfect pangram," to mean any sentence where every letter appears n times for some positive n). And it was a fun challenge. But recently it got me wondering whether it would be possible to solve this challenge purely with regular expressions. If it is, I expect it will be a pretty gnarly regex. But I'm not even sure it is possible.
Still, if anyone is able to do it, it would be the folks here at Code Golf. So here's the challenge:
Write a regular expression which can determine whether or not a given string is a pangram. You can assume that all non-alpha characters have already been stripped out (or don't, if you really want to challenge yourself), and that everything has been transformed into lowercase.
Bonus challenge: write another regular expression to determine whether the given string is a "perfect pangram" by the definition given above.
Examples of an ordinary pangram:
thequickbrownfoxjumpsoverthelazydog
Example of perfect pangrams:
abcdefghijklmnopqrstuvwxyz
jocknymphswaqfdrugvexblitz
squdgykilpjobzarfnthcwmvexhmfjordwaltzcinqbuskpyxveg
I'm not really sure if this is even possible just with regex alone. If you find that it is, kudos!
regular-expression
2
Pity that Martin is on leave
â Luis Mendo
4 hours ago
1
Pangram, not anagram
â SoaperGEM
4 hours ago
Quoted verbatim from the question above (read the question): "any sentence where every letter appears n times for some positive n."
â SoaperGEM
4 hours ago
Ah, sorry. Still, you need to address the other issues
â Luis Mendo
4 hours ago
4
Let me rephrase, as my previous comment was a mess: 1) This needs a winning criterion. Code golf? 2) Bonuses are almost always a bad idea. There should be a single task/objective to the challenge. Having two is confusing, and probably useless, as people will go fot the easier one. 3) What flavours of regexes are allowed?
â Luis Mendo
4 hours ago
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
Awhile back I encountered a simple programming puzzle to determine whether a string was a pangram, or a perfect pangram. (Although it's worth noting that this particular challenge gave a different meaning than the standard definition of a "perfect pangram," to mean any sentence where every letter appears n times for some positive n). And it was a fun challenge. But recently it got me wondering whether it would be possible to solve this challenge purely with regular expressions. If it is, I expect it will be a pretty gnarly regex. But I'm not even sure it is possible.
Still, if anyone is able to do it, it would be the folks here at Code Golf. So here's the challenge:
Write a regular expression which can determine whether or not a given string is a pangram. You can assume that all non-alpha characters have already been stripped out (or don't, if you really want to challenge yourself), and that everything has been transformed into lowercase.
Bonus challenge: write another regular expression to determine whether the given string is a "perfect pangram" by the definition given above.
Examples of an ordinary pangram:
thequickbrownfoxjumpsoverthelazydog
Example of perfect pangrams:
abcdefghijklmnopqrstuvwxyz
jocknymphswaqfdrugvexblitz
squdgykilpjobzarfnthcwmvexhmfjordwaltzcinqbuskpyxveg
I'm not really sure if this is even possible just with regex alone. If you find that it is, kudos!
regular-expression
Awhile back I encountered a simple programming puzzle to determine whether a string was a pangram, or a perfect pangram. (Although it's worth noting that this particular challenge gave a different meaning than the standard definition of a "perfect pangram," to mean any sentence where every letter appears n times for some positive n). And it was a fun challenge. But recently it got me wondering whether it would be possible to solve this challenge purely with regular expressions. If it is, I expect it will be a pretty gnarly regex. But I'm not even sure it is possible.
Still, if anyone is able to do it, it would be the folks here at Code Golf. So here's the challenge:
Write a regular expression which can determine whether or not a given string is a pangram. You can assume that all non-alpha characters have already been stripped out (or don't, if you really want to challenge yourself), and that everything has been transformed into lowercase.
Bonus challenge: write another regular expression to determine whether the given string is a "perfect pangram" by the definition given above.
Examples of an ordinary pangram:
thequickbrownfoxjumpsoverthelazydog
Example of perfect pangrams:
abcdefghijklmnopqrstuvwxyz
jocknymphswaqfdrugvexblitz
squdgykilpjobzarfnthcwmvexhmfjordwaltzcinqbuskpyxveg
I'm not really sure if this is even possible just with regex alone. If you find that it is, kudos!
regular-expression
regular-expression
asked 4 hours ago
SoaperGEM
1788
1788
2
Pity that Martin is on leave
â Luis Mendo
4 hours ago
1
Pangram, not anagram
â SoaperGEM
4 hours ago
Quoted verbatim from the question above (read the question): "any sentence where every letter appears n times for some positive n."
â SoaperGEM
4 hours ago
Ah, sorry. Still, you need to address the other issues
â Luis Mendo
4 hours ago
4
Let me rephrase, as my previous comment was a mess: 1) This needs a winning criterion. Code golf? 2) Bonuses are almost always a bad idea. There should be a single task/objective to the challenge. Having two is confusing, and probably useless, as people will go fot the easier one. 3) What flavours of regexes are allowed?
â Luis Mendo
4 hours ago
add a comment |Â
2
Pity that Martin is on leave
â Luis Mendo
4 hours ago
1
Pangram, not anagram
â SoaperGEM
4 hours ago
Quoted verbatim from the question above (read the question): "any sentence where every letter appears n times for some positive n."
â SoaperGEM
4 hours ago
Ah, sorry. Still, you need to address the other issues
â Luis Mendo
4 hours ago
4
Let me rephrase, as my previous comment was a mess: 1) This needs a winning criterion. Code golf? 2) Bonuses are almost always a bad idea. There should be a single task/objective to the challenge. Having two is confusing, and probably useless, as people will go fot the easier one. 3) What flavours of regexes are allowed?
â Luis Mendo
4 hours ago
2
2
Pity that Martin is on leave
â Luis Mendo
4 hours ago
Pity that Martin is on leave
â Luis Mendo
4 hours ago
1
1
Pangram, not anagram
â SoaperGEM
4 hours ago
Pangram, not anagram
â SoaperGEM
4 hours ago
Quoted verbatim from the question above (read the question): "any sentence where every letter appears n times for some positive n."
â SoaperGEM
4 hours ago
Quoted verbatim from the question above (read the question): "any sentence where every letter appears n times for some positive n."
â SoaperGEM
4 hours ago
Ah, sorry. Still, you need to address the other issues
â Luis Mendo
4 hours ago
Ah, sorry. Still, you need to address the other issues
â Luis Mendo
4 hours ago
4
4
Let me rephrase, as my previous comment was a mess: 1) This needs a winning criterion. Code golf? 2) Bonuses are almost always a bad idea. There should be a single task/objective to the challenge. Having two is confusing, and probably useless, as people will go fot the easier one. 3) What flavours of regexes are allowed?
â Luis Mendo
4 hours ago
Let me rephrase, as my previous comment was a mess: 1) This needs a winning criterion. Code golf? 2) Bonuses are almost always a bad idea. There should be a single task/objective to the challenge. Having two is confusing, and probably useless, as people will go fot the easier one. 3) What flavours of regexes are allowed?
â Luis Mendo
4 hours ago
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
3
down vote
Assuming you're using a flavor of regex that include look-aheads, detecting a pangram is straightforward, but tedious. Using Python syntax:
r"(?=.*a)(?=.*b)(?=.*c)(?=.*d)(?=.*e)(?=.*f)(?=.*g)(?=.*h)(?=.*i)(?=.*j)(?=.*k)(?=.*l)(?=.*m)(?=.*n)(?=.*o)(?=.*p)(?=.*q)(?=.*r)(?=.*s)(?=.*t)(?=.*u)(?=.*v)(?=.*w)(?=.*x)(?=.*y)(?=.*z).*"
https://regex101.com/r/Z7NjEQ/2
The (?=.*a)
block tests if what follows after that point matches .*a
; another way to say that is, is this expression followed by any characters and then an a
, or just, does this include an a
? Then repeat that check for every letter of the alphabet.
Without lookaheads, it's still possible, but even more tedious, because you have to account for every possible ordering of the alphabet. If you were limited to the letters abc
, here's what it might look like:
r".*(a.*b.*c|a.*c.*b|b.*a.*c|b.*c.*a|c.*a.*b|c.*b.*a).*"
https://regex101.com/r/OnXQe7/1/
This checks for an a
, any set of characters, a b
, any set of characters, then a c
, OR an a
, any set of characters, a c
, any set of characters, then a b
, OR...you get the idea.
You could use the same approach to check for the standard meaning of perfect pangram (every character exactly once), but not the modified version used here.
add a comment |Â
up vote
0
down vote
Regexp: 45 43 20 19 18 bytes
(?!.*(.).*1).26
Assuming the input is [a-z] (lowercase, only alphabets a to z, spaces etc removed)
A byte saved, thanks to Neil
1
If you're assuming the input format then.
instead ofw
?
â Neil
3 hours ago
Better to ask for confirmation/clarification rather than assuming anything.
â Shaggy
3 hours ago
@Neil oh, thanks, stupid me
â FatalError
2 hours ago
@Shaggy "You can assume that all non-alpha characters have already been stripped out", just notified about the assumption
â FatalError
2 hours ago
This doesnâÂÂt work for either the regular challenge or the bonus. DoesnâÂÂt matchthequickbrownfoxjumpsoverthelazydog
(regular pangram) orsqudgykil...
(the bonus).
â FrownyFrog
1 hour ago
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
Assuming you're using a flavor of regex that include look-aheads, detecting a pangram is straightforward, but tedious. Using Python syntax:
r"(?=.*a)(?=.*b)(?=.*c)(?=.*d)(?=.*e)(?=.*f)(?=.*g)(?=.*h)(?=.*i)(?=.*j)(?=.*k)(?=.*l)(?=.*m)(?=.*n)(?=.*o)(?=.*p)(?=.*q)(?=.*r)(?=.*s)(?=.*t)(?=.*u)(?=.*v)(?=.*w)(?=.*x)(?=.*y)(?=.*z).*"
https://regex101.com/r/Z7NjEQ/2
The (?=.*a)
block tests if what follows after that point matches .*a
; another way to say that is, is this expression followed by any characters and then an a
, or just, does this include an a
? Then repeat that check for every letter of the alphabet.
Without lookaheads, it's still possible, but even more tedious, because you have to account for every possible ordering of the alphabet. If you were limited to the letters abc
, here's what it might look like:
r".*(a.*b.*c|a.*c.*b|b.*a.*c|b.*c.*a|c.*a.*b|c.*b.*a).*"
https://regex101.com/r/OnXQe7/1/
This checks for an a
, any set of characters, a b
, any set of characters, then a c
, OR an a
, any set of characters, a c
, any set of characters, then a b
, OR...you get the idea.
You could use the same approach to check for the standard meaning of perfect pangram (every character exactly once), but not the modified version used here.
add a comment |Â
up vote
3
down vote
Assuming you're using a flavor of regex that include look-aheads, detecting a pangram is straightforward, but tedious. Using Python syntax:
r"(?=.*a)(?=.*b)(?=.*c)(?=.*d)(?=.*e)(?=.*f)(?=.*g)(?=.*h)(?=.*i)(?=.*j)(?=.*k)(?=.*l)(?=.*m)(?=.*n)(?=.*o)(?=.*p)(?=.*q)(?=.*r)(?=.*s)(?=.*t)(?=.*u)(?=.*v)(?=.*w)(?=.*x)(?=.*y)(?=.*z).*"
https://regex101.com/r/Z7NjEQ/2
The (?=.*a)
block tests if what follows after that point matches .*a
; another way to say that is, is this expression followed by any characters and then an a
, or just, does this include an a
? Then repeat that check for every letter of the alphabet.
Without lookaheads, it's still possible, but even more tedious, because you have to account for every possible ordering of the alphabet. If you were limited to the letters abc
, here's what it might look like:
r".*(a.*b.*c|a.*c.*b|b.*a.*c|b.*c.*a|c.*a.*b|c.*b.*a).*"
https://regex101.com/r/OnXQe7/1/
This checks for an a
, any set of characters, a b
, any set of characters, then a c
, OR an a
, any set of characters, a c
, any set of characters, then a b
, OR...you get the idea.
You could use the same approach to check for the standard meaning of perfect pangram (every character exactly once), but not the modified version used here.
add a comment |Â
up vote
3
down vote
up vote
3
down vote
Assuming you're using a flavor of regex that include look-aheads, detecting a pangram is straightforward, but tedious. Using Python syntax:
r"(?=.*a)(?=.*b)(?=.*c)(?=.*d)(?=.*e)(?=.*f)(?=.*g)(?=.*h)(?=.*i)(?=.*j)(?=.*k)(?=.*l)(?=.*m)(?=.*n)(?=.*o)(?=.*p)(?=.*q)(?=.*r)(?=.*s)(?=.*t)(?=.*u)(?=.*v)(?=.*w)(?=.*x)(?=.*y)(?=.*z).*"
https://regex101.com/r/Z7NjEQ/2
The (?=.*a)
block tests if what follows after that point matches .*a
; another way to say that is, is this expression followed by any characters and then an a
, or just, does this include an a
? Then repeat that check for every letter of the alphabet.
Without lookaheads, it's still possible, but even more tedious, because you have to account for every possible ordering of the alphabet. If you were limited to the letters abc
, here's what it might look like:
r".*(a.*b.*c|a.*c.*b|b.*a.*c|b.*c.*a|c.*a.*b|c.*b.*a).*"
https://regex101.com/r/OnXQe7/1/
This checks for an a
, any set of characters, a b
, any set of characters, then a c
, OR an a
, any set of characters, a c
, any set of characters, then a b
, OR...you get the idea.
You could use the same approach to check for the standard meaning of perfect pangram (every character exactly once), but not the modified version used here.
Assuming you're using a flavor of regex that include look-aheads, detecting a pangram is straightforward, but tedious. Using Python syntax:
r"(?=.*a)(?=.*b)(?=.*c)(?=.*d)(?=.*e)(?=.*f)(?=.*g)(?=.*h)(?=.*i)(?=.*j)(?=.*k)(?=.*l)(?=.*m)(?=.*n)(?=.*o)(?=.*p)(?=.*q)(?=.*r)(?=.*s)(?=.*t)(?=.*u)(?=.*v)(?=.*w)(?=.*x)(?=.*y)(?=.*z).*"
https://regex101.com/r/Z7NjEQ/2
The (?=.*a)
block tests if what follows after that point matches .*a
; another way to say that is, is this expression followed by any characters and then an a
, or just, does this include an a
? Then repeat that check for every letter of the alphabet.
Without lookaheads, it's still possible, but even more tedious, because you have to account for every possible ordering of the alphabet. If you were limited to the letters abc
, here's what it might look like:
r".*(a.*b.*c|a.*c.*b|b.*a.*c|b.*c.*a|c.*a.*b|c.*b.*a).*"
https://regex101.com/r/OnXQe7/1/
This checks for an a
, any set of characters, a b
, any set of characters, then a c
, OR an a
, any set of characters, a c
, any set of characters, then a b
, OR...you get the idea.
You could use the same approach to check for the standard meaning of perfect pangram (every character exactly once), but not the modified version used here.
edited 4 hours ago
answered 4 hours ago
Jack Brounstein
36116
36116
add a comment |Â
add a comment |Â
up vote
0
down vote
Regexp: 45 43 20 19 18 bytes
(?!.*(.).*1).26
Assuming the input is [a-z] (lowercase, only alphabets a to z, spaces etc removed)
A byte saved, thanks to Neil
1
If you're assuming the input format then.
instead ofw
?
â Neil
3 hours ago
Better to ask for confirmation/clarification rather than assuming anything.
â Shaggy
3 hours ago
@Neil oh, thanks, stupid me
â FatalError
2 hours ago
@Shaggy "You can assume that all non-alpha characters have already been stripped out", just notified about the assumption
â FatalError
2 hours ago
This doesnâÂÂt work for either the regular challenge or the bonus. DoesnâÂÂt matchthequickbrownfoxjumpsoverthelazydog
(regular pangram) orsqudgykil...
(the bonus).
â FrownyFrog
1 hour ago
add a comment |Â
up vote
0
down vote
Regexp: 45 43 20 19 18 bytes
(?!.*(.).*1).26
Assuming the input is [a-z] (lowercase, only alphabets a to z, spaces etc removed)
A byte saved, thanks to Neil
1
If you're assuming the input format then.
instead ofw
?
â Neil
3 hours ago
Better to ask for confirmation/clarification rather than assuming anything.
â Shaggy
3 hours ago
@Neil oh, thanks, stupid me
â FatalError
2 hours ago
@Shaggy "You can assume that all non-alpha characters have already been stripped out", just notified about the assumption
â FatalError
2 hours ago
This doesnâÂÂt work for either the regular challenge or the bonus. DoesnâÂÂt matchthequickbrownfoxjumpsoverthelazydog
(regular pangram) orsqudgykil...
(the bonus).
â FrownyFrog
1 hour ago
add a comment |Â
up vote
0
down vote
up vote
0
down vote
Regexp: 45 43 20 19 18 bytes
(?!.*(.).*1).26
Assuming the input is [a-z] (lowercase, only alphabets a to z, spaces etc removed)
A byte saved, thanks to Neil
Regexp: 45 43 20 19 18 bytes
(?!.*(.).*1).26
Assuming the input is [a-z] (lowercase, only alphabets a to z, spaces etc removed)
A byte saved, thanks to Neil
edited 2 hours ago
answered 3 hours ago
FatalError
592
592
1
If you're assuming the input format then.
instead ofw
?
â Neil
3 hours ago
Better to ask for confirmation/clarification rather than assuming anything.
â Shaggy
3 hours ago
@Neil oh, thanks, stupid me
â FatalError
2 hours ago
@Shaggy "You can assume that all non-alpha characters have already been stripped out", just notified about the assumption
â FatalError
2 hours ago
This doesnâÂÂt work for either the regular challenge or the bonus. DoesnâÂÂt matchthequickbrownfoxjumpsoverthelazydog
(regular pangram) orsqudgykil...
(the bonus).
â FrownyFrog
1 hour ago
add a comment |Â
1
If you're assuming the input format then.
instead ofw
?
â Neil
3 hours ago
Better to ask for confirmation/clarification rather than assuming anything.
â Shaggy
3 hours ago
@Neil oh, thanks, stupid me
â FatalError
2 hours ago
@Shaggy "You can assume that all non-alpha characters have already been stripped out", just notified about the assumption
â FatalError
2 hours ago
This doesnâÂÂt work for either the regular challenge or the bonus. DoesnâÂÂt matchthequickbrownfoxjumpsoverthelazydog
(regular pangram) orsqudgykil...
(the bonus).
â FrownyFrog
1 hour ago
1
1
If you're assuming the input format then
.
instead of w
?â Neil
3 hours ago
If you're assuming the input format then
.
instead of w
?â Neil
3 hours ago
Better to ask for confirmation/clarification rather than assuming anything.
â Shaggy
3 hours ago
Better to ask for confirmation/clarification rather than assuming anything.
â Shaggy
3 hours ago
@Neil oh, thanks, stupid me
â FatalError
2 hours ago
@Neil oh, thanks, stupid me
â FatalError
2 hours ago
@Shaggy "You can assume that all non-alpha characters have already been stripped out", just notified about the assumption
â FatalError
2 hours ago
@Shaggy "You can assume that all non-alpha characters have already been stripped out", just notified about the assumption
â FatalError
2 hours ago
This doesnâÂÂt work for either the regular challenge or the bonus. DoesnâÂÂt match
thequickbrownfoxjumpsoverthelazydog
(regular pangram) or squdgykil...
(the bonus).â FrownyFrog
1 hour ago
This doesnâÂÂt work for either the regular challenge or the bonus. DoesnâÂÂt match
thequickbrownfoxjumpsoverthelazydog
(regular pangram) or squdgykil...
(the bonus).â FrownyFrog
1 hour ago
add a comment |Â
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
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f172809%2fregular-expression-to-find-a-pangram%23new-answer', 'question_page');
);
Post as a guest
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
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
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
2
Pity that Martin is on leave
â Luis Mendo
4 hours ago
1
Pangram, not anagram
â SoaperGEM
4 hours ago
Quoted verbatim from the question above (read the question): "any sentence where every letter appears n times for some positive n."
â SoaperGEM
4 hours ago
Ah, sorry. Still, you need to address the other issues
â Luis Mendo
4 hours ago
4
Let me rephrase, as my previous comment was a mess: 1) This needs a winning criterion. Code golf? 2) Bonuses are almost always a bad idea. There should be a single task/objective to the challenge. Having two is confusing, and probably useless, as people will go fot the easier one. 3) What flavours of regexes are allowed?
â Luis Mendo
4 hours ago