10
$\begingroup$
s = "1 2 ";
StringCases[s, (n : NumberString ~~ " ") .. ]
StringCases[s, (NumberString ~~ " ") .. ]

yields

{"1 ", "2 "}
{"1 2 "}

Why?

$\endgroup$
5
  • $\begingroup$ If you parenthesize, as in (n : NumberString), what happens? $\endgroup$ Jun 16, 2015 at 14:11
  • 1
    $\begingroup$ You may want to analyze the output of StringPattern`PatternConvert[pattern] in both cases $\endgroup$ Jun 16, 2015 at 14:46
  • $\begingroup$ what would n be set to if the second case was the result? $\endgroup$
    – george2079
    Jun 16, 2015 at 15:27
  • 3
    $\begingroup$ By naming the number string n in the first situation you are specifying that only patterns of the form (n~~" ").. will match, where the repeating element must contain the same number n. Since 2!=1 you don't match any further. In the second case you allow any number digit in the repeated part of the pattern. Try matching 1 1 . $\endgroup$
    – N.J.Evans
    Jun 16, 2015 at 15:44
  • $\begingroup$ @N.J.Evans That sounds like an answer to me $\endgroup$ Jun 16, 2015 at 16:20

1 Answer 1

16
$\begingroup$

Patterns get confusing quickly. If you name a pattern you're imposing more restrictions on that pattern that are sometimes difficult to follow. Using your example,

s = "1 2 ";
StringCases[s, (n : NumberString ~~ " ") .. ]
StringCases[s, (NumberString ~~ " ") .. ]

In the first case you're telling string cases to match n, where n must be a number string and to continue the pattern for any match with the parenthetical statement repeated. In the repeated suffix n must always be the same number!

In the second case you're specifying that any repeated pattern of NumberString+Whitespace should match. Since you haven't named the number string, the pattern still applies generally to any number.

Trying

 s = "1 1 1 2 2";
 StringCases[s, (n : NumberString ~~ " ") .. ]
 StringCases[s, (NumberString ~~ " ") .. ]

Will give:

{1 1 1 , 2 2 }
{1 1 1 2 2 }

Which shows that the first pattern works any time the integer following the white space is the same as the n that triggered the match.

$\endgroup$
2
  • 2
    $\begingroup$ (+1) Here is an example which demonstrates that this behavior is consistent with the usual behavior of the Mathematica's pattern-matcher: {MatchQ[{1, 1, 2}, {x_Integer ..}], MatchQ[{1, 1, 2}, {_Integer ..}]}. $\endgroup$ Jun 16, 2015 at 16:36
  • $\begingroup$ And this is the correct answer. I'm not sure why I did not see this because in normal patterns like {x_Integer..} it is completely clear and I use it all of the time. $\endgroup$
    – halirutan
    Jun 16, 2015 at 16:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.