Is there a regexp which would find longest common prefix of two strings? And if this is not solvable by one regexp, what would be the most elegant piece of code or oneliner using regexp (perl, ruby, python, anything).
PS: I can do this easily programatically, I am asking rather for curiosity, because it seems to me that this could be solveable by regexp.
PPS: Extra bonus for O(n) solution using regexps. Come on, it should exist!
If there's some character that neither string contains —, say,
\0 — you could write
"$first\0$second" =~ m/^(.*).*\0\1/s;
and the longest common prefix would be saved as
Edited to add: This is obviously very inefficient. I think that if efficiency is a concern, then this simply isn't the approach we should be using; but we can at least improve it by changing
[^\0]* to prevent useless greediness that will just have to be backtracked again, and wrapping the second
(?>…) to prevent backtracking that can't help. This:
"$first\0$second" =~ m/^([^\0]*)(?>[^\0]*)\0\1/s;
This will yield the same result, but much more efficiently. (But still not nearly as efficiently as a straightforward non–regex-based approach. If the strings both have length n, I'd expect its worst case to take at least O(n2) time, whereas the straightforward non–regex-based approach would take O(n) time in its worst case.)
Here's a Python one-liner:
>>> a = 'stackoverflow' >>> b = 'stackofpancakes' >>> a[:[x==x for x in zip(a,b)].index(0)] 0: 'stacko' >>> a = 'nothing in' >>> b = 'common' >>> a[:[x==x for x in zip(a,b)].index(0)] 1: '' >>>