Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

C#

Marko Kallaspoolik
Marko Kallaspoolik
10,455 Points

Does anybody know how to get matching letter combinations from a string?

I was wondering if it is possible to create a program that would return 3+ letter combinations from a string? For example if I have something like this "Hello world, hello" It would return: Hello - 2 matches, ello - 2 matches, world - 1 match, orld - 1 match

Any help would be appreciated!! Thank you!

1 Answer

Allan Clark
Allan Clark
10,810 Points

This is definitely possible but likely more complex than it looks at first glance. Regular Expression will be your best bet I believe, so you will likely need to use the Regex.Matches() and maybe some string comparisons on those Matches. Forewarning, building a Regex pattern is pretty much its own language so it will require some research.

The requirements given are more complex than they appear on the surface so you may need to tack them down a little better. For example, you say you want it to return world - 1 match, orld - 1, that implies you are looking match every substring that is 3 or more characters, but also the specific number of characters as well. (i.e. wor, orl, rld,worl,orld,world all match this criteria and would need to be counted separately)

For a starting point consider the following Regex expressions: (you can test these at https://regexr.com/)

Regex.Matches(@"(\w{3,})") //this will return all that are 3 *or more* 

Regex.Matches(@"(\w{3})") //this will return all that are exactly 3

Your big issue is going to be dealing with overlapping groups. The first regex will only return the largest group (i.e. "world" would be returned but not "wor"). The second does not deal with overlap (i.e. "wor" would be returned but not "orl" or "rld").

Hope this helps, Happy Coding!