'Different spellings of Chanukah Regex [closed]
Hannuka, Chanukah, Hanukkah...Due to transliteration from another language and character set, there are many ways to spell the name of this holiday. How many legitimate spellings can you come up with?
Now, write a regular expression that will recognise all of them.
Solution 1:[1]
According to http://www.holidays.net/chanukah/spelling.htm, it can be spelled any of the following ways:
Chanuka
Chanukah
Chanukkah
Channukah
Hanukah
Hannukah
Hanukkah
Hanuka
Hanukka
Hanaka
Haneka
Hanika
Khanukkah
Here is my regex that matches all of them:
/(Ch|H|Kh)ann?[aeiu]kk?ah?/
Edit: Or this, without branches:
/[CHK]h?ann?[aeiu]kk?ah?/
Solution 2:[2]
Call me a sucker for readability.
In Python:
def find_hanukkah(s):
import re
spellings = ['hannukah', 'channukah', 'hanukkah'] # etc...
for m in re.finditer('|'.join(spellings), s, re.I):
print m.group()
find_hanukkah("Hannukah Channukah, Hanukkah")
Solution 3:[3]
Something like C?hann?uk?kah? matches most of the common cases. There also a bunch of weirder spellings C?hann?uk?kah?|Han[aei]ka|Khanukkah matches almost every spelling I could think of (that had at least half a million hits on google).
Solution 4:[4]
Try this:
/^[ck]?hann?ukk?ah?$/i
Solution 5:[5]
I think the only approved spellings in English are Hanukkah and Chanukh, so it's something like
/(Ch|H)anuk?kah/
Or maybe even better
/(Chanukah|Hanukkah)/
Solution 6:[6]
I like Triptych's answer, but i would take it one step forward... also in python:
def valid(spelling):
import re
regex_spelling = re.compile(r'^[cCkK]{0,1}han{1,2}uk{1,2}ah$')
valid = regex_spelling.match(spelling)
if valid:
print 'Valid spelling'
else:
print spelling, " is not a spelling for the word"
to use it:
valid("hanukkah")
Solution 7:[7]
((Ch|H|X|?|Kh|J)[a?](?|n{1,2})(?|ou|[auei])(?|k|q){1,2}[a?]h?)|(??????)
This regex is much more inclusive and covers all of the following options:
Channuka Channukah Channukka Channukkah Chanuka Chanukah Chanukah Chanukka Chanukkah Chanuqa Hanaka Haneka Hanika Hannuka Hannukah Hannukka Hannukkah Hanoukka Hanuka Hanukah Hanukka Hanukkah Januka Khanukkah Xanuka ?????? ?????? ????? ????
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Paige Ruten |
| Solution 2 | |
| Solution 3 | |
| Solution 4 | A Pars |
| Solution 5 | Charlie Martin |
| Solution 6 | Robert S. |
| Solution 7 | Liron |
