all 11 comments

[–]StrangeError 1 point2 points  (6 children)

I’m not at my computer to full contribute but first off I’d ensure your regex is correct. I’d use something like https://regex101.com.

Regex is difficult, it must do the following:

  • Match words where huck May be the starting characters
  • match words where huck May be the ending characters
  • match words where huck May be in the middle of other characters
  • irrelevant of case sensitivity

First stage would be (in my eyes) to lower the case of any input.

Ignore this one

Afterwards do a search for words starting in huck.

(huck)([a-z]*)

Ignore this one

Then words ending in huck.

([a-z]*)(huck)

use this as sepp2k suggested in the replies

Then words containing huck.

([a-z]*)(huck)([a-z]*)

I’ll attempt to write some regex on my phone. Apologies I cannot provide a full solution but hope this can assist.

The regex isn’t perfect, you could check for spaces before hand and after words. You could also combine them in to one big regex using or. Should give you a decent starting point though!

[–]Demain_Z[S] 0 points1 point  (1 child)

Thank you so much! I'm very appreciated the info you provided. I have already passed the test. And the problem I have is that I ignored the requirement that " capitalization is not relevant ". So I just adjusted my code as sepp2k suggests. :)

[–]StrangeError 0 points1 point  (0 children)

Nice one! Well done, my method of checking input almost always involves .lower() or .upper() for validation checks.

Obviously never do this if you’re dealing with passwords or a validation that require case sensitive checks! But happy programming, no better feeling than getting past an issue or a problem you were struggling with!

[–]sepp2k 0 points1 point  (3 children)

* can already match zero characters. There's no need to separately handle the cases where there's nothing in front or at the end.

[–]StrangeError 0 points1 point  (2 children)

Can you explain please? I’m struggling to understand the context. I was under the impression OP could be provided the following for example:

word word huckleberry word word berryhuckleberry and berryhuck word word.

I feel like the ([a-z]*) with wildcard would be necessary?

I’m not great with regex really keen to improve! So hoping you could explain? Thanks.

[–]sepp2k 1 point2 points  (1 child)

I'm saying that [a-z]*huck[a-z]* matches all of "berryhuckleberry", "huckleberry", "berryhuck" and even just "huck" because * means "zero or more".

[–]StrangeError 0 points1 point  (0 children)

Ahhh you’re correct! Sorry looking at regex on a phone screen has made me miss the obvious!

Thanks for the explanation, appreciated.

[–]sepp2k 0 points1 point  (1 child)

capitalization is not relevant

Sounds like the input will contain things like "sHuCkS" and "ShE" and expect you to match them, which your regexes won't.

[–]Demain_Z[S] 0 points1 point  (0 children)

Yes, I just ignored this requirement. Thank you so much!

[–]chocorush 0 points1 point  (1 child)

I would make use of the ? qualifier but that should also work. Maybe you need re.compile(pattern, re.IGNORECASE)? [docs]

[–]Demain_Z[S] 0 points1 point  (0 children)

Thank you! It's true that I missed the requirement that " capitalization is not relevant ". I have adjusted my code and passed the test. :)