0

How do I create a maximal glob from a list of strings?

Input:

['onething.txt', 'anotherthing.txt']

Output:

*o*thing.txt

or

*thing.txt

Whichever is more correct.

martineau
  • 119,623
  • 25
  • 170
  • 301
jaksco
  • 423
  • 7
  • 18
  • 1
    This sounds very similar to the [longest common substring problem](https://en.wikipedia.org/wiki/Longest_common_substring_problem) to me, so maybe using one of algorithms for solving would help (or finding a modules that does). – martineau Feb 04 '22 at 02:52
  • 1
    What exactly do you consider "maximal" or "more correct"? `[oa]n*e*thing.txt` is the most specific pattern I can come up with given the limitations of globs, but that still matches lots of potential filenames other than the two inputs. – jasonharper Feb 04 '22 at 02:58
  • For this situation I'm not too picky. Something like `*n*e*thing.txt` would be fine. It looks like `difflib` has some utilities to handle this. I would prefer more than one matching substring segment (not only the longest match; in this case thing.txt) – jaksco Feb 04 '22 at 03:01
  • `os.path.commonprefix` is cool. I didn't know that existed. That might be good enough for my situation actually... – jaksco Feb 04 '22 at 03:05
  • 1
    I think I'll go ahead and close this question because there are many answers already. I just wasn't searching google for "common substring" – jaksco Feb 04 '22 at 03:12
  • https://stackoverflow.com/questions/18715688/find-common-substring-between-two-strings – jaksco Feb 04 '22 at 03:12
  • https://stackoverflow.com/questions/2892931/longest-common-substring-from-more-than-two-strings – jaksco Feb 04 '22 at 03:12

0 Answers0