KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
Suppose you have a string (e.g. needle ). Its 19 continuous substrings are: needle needl eedle need eedl edle nee eed edl dle ne ee ed dl le n e d l If I were to build a regex to match, in a haystack, any of the substrings I could simply do: /(needle|needl|eedle|need|eedl|edle|nee|eed|edl|dle|ne|ee|ed|dl|le|n|e|d|l)/ but it doesn't look really elegant. Is there a better way to create a regex that will greedly match any one of the substrings of a given string? Additionally, what if I posed another constraint, wanted to match only substrings longer than a threshold, e.g. for substrings of at least 3 characters: /(needle|needl|eedle|need|eedl|edle|nee|eed|edl|dle)/ note: I deliberately did not mention any particular regex dialect. Please state which one you're using in your answer.
Tags (comma-separated)
Save Edits
Cancel