Skip to main content
edited tags
Link
Alexey Popkov
  • 62.5k
  • 7
  • 163
  • 405
Notice removed Reward existing answer by Mr.Wizard
Bounty Ended with WReach's answer chosen by Mr.Wizard
Notice added Reward existing answer by Mr.Wizard
Bounty Started worth 100 reputation by Mr.Wizard
Tweeted twitter.com/StackMma/status/702838229641404416
Source Link
A Simmons
  • 412
  • 4
  • 9

Unexpected behaviour when pattern matching with Longest

I came across some unexpected behaviour today when using the Longest function when trying to do some pattern matching.

StringCases[#, a___ ~~ b_ ~~ Longest[c___] ~~ b_ ~~ d___ -> {a, b, c, d}] &@"abcbba" 

The intent is to find the two identical characters which are the furthest apart in the string. I would expect this to return {a,b,cb,a}, since that would be the longest possible distance between two identical characters. However, it instead returns {abc,b, ,a}. I don't understand why it's not finding the longer possibility, especially when default behaviour when matching patterns seems to be that earlier patterns try to match the shortest possible sequences. Using c__ instead of c___ makes the behaviour more like the expected behaviour for this case, but I do want the pattern to work when the only sets of identical characters are adjacent to each other.

What am I missing?