Regex matching string containing specific letters | Sololearn: Learn to code for FREE!
0

Regex matching string containing specific letters

I have a regular expression that'll match all strings containing the letters 'r' and 'h': /(.*h.*r.*|.*r.*h.*)/ The problem with this method is that it gets very long pretty quickly. If it was 3 letters it would look like this: /(.*h.*r.*k.*|.*h.*k.*r.*|.*r.*k.*h.*|.*r.*h.*k.*|.*k.*h.*r.*|.*k.*r.*h.*)/ Isn't there a simpler way to do this? It just has to match a string containing 1 or more of all letters

4th Oct 2018, 4:25 PM
ReimarPB
ReimarPB - avatar
3 Answers
+ 1
r'\w*[hrk]+\w*' => any amount (including 0) of other letters, at least one h, r, or k and 0 or any amount of other letters again
4th Oct 2018, 5:13 PM
Anna
Anna - avatar
0
Anna where can we learn more about regular expressions
4th Oct 2018, 7:35 PM
Infinite Loop
Infinite Loop - avatar
0
Milan Adhikari You can try Sololearn's tutorial: https://www.sololearn.com/learn/9704/?ref=app There are good resources on the web, too
6th Oct 2018, 7:18 AM
Anna
Anna - avatar