Skip to main content
replaced http://mathematica.stackexchange.com/ with https://mathematica.stackexchange.com/
Source Link

More explicitly we can use Print or Sow as the test function(1)(1) to see exactly which expressions are being tested:

The first point is actually very useful behavior and I direct you to my own answer Using a PatternTest versus a Condition for pattern matchingUsing a PatternTest versus a Condition for pattern matching for additional examples.

More explicitly we can use Print or Sow as the test function(1) to see exactly which expressions are being tested:

The first point is actually very useful behavior and I direct you to my own answer Using a PatternTest versus a Condition for pattern matching for additional examples.

More explicitly we can use Print or Sow as the test function(1) to see exactly which expressions are being tested:

The first point is actually very useful behavior and I direct you to my own answer Using a PatternTest versus a Condition for pattern matching for additional examples.

edited body
Source Link
Mr.Wizard
  • 273.1k
  • 34
  • 595
  • 1.4k

The second point inis the deleterious consequence of extremely flexible pattern matching used in Mathematica which allows the test function itself to be stateful. I personally feel that there should be a more efficient matching scheme available as an alternative as many uses do not require this level of generality.

The second point in the deleterious consequence of extremely flexible pattern matching used in Mathematica which allows the test function itself to be stateful. I personally feel that there should be a more efficient matching scheme available as an alternative as many uses do not require this level of generality.

The second point is the deleterious consequence of extremely flexible pattern matching used in Mathematica which allows the test function itself to be stateful. I personally feel that there should be a more efficient matching scheme available as an alternative as many uses do not require this level of generality.

Source Link
Mr.Wizard
  • 273.1k
  • 34
  • 595
  • 1.4k

You commented on July 6th:

but I don't understand still why the ? cannot work totally and give whole string.

As MarcoB already quoted:

In a form such as __?test, every element in the sequence matched by __ must yield True when test is applied.

You can easily see for yourself that this is true.

words = {"is", "a", "problem"};

StringCases["What is the best approach to a problem?", __?(MemberQ[words, #] &)]
{"a", "a", "a", "a"}

More explicitly we can use Print or Sow as the test function(1) to see exactly which expressions are being tested:

Reap[ StringCases["Mathematica", __?Sow] ][[2, 1]]
{"M", "M", "M", "M", "M", "M", "M", "M", "M", "M", "M", "a", "a", "a", "a", "a", 
"a", "a", "a", "a", "a", "t", "t", "t", "t", "t", "t", "t", "t", "t", "h", "h", 
"h", "h", "h", "h", "h", "h", "e", "e", "e", "e", "e", "e", "e", "m", "m", "m", 
"m", "m", "m", "a", "a", "a", "a", "a", "t", "t", "t", "t", "i", "i", "i", "c", 
"c", "a"}

Observe that:

  1. Only single letter strings are ever tested
  2. 66 matches are attempted due to every test failing (11 + 10 + 9 + 8 ...)

The first point is actually very useful behavior and I direct you to my own answer Using a PatternTest versus a Condition for pattern matching for additional examples.

The second point in the deleterious consequence of extremely flexible pattern matching used in Mathematica which allows the test function itself to be stateful. I personally feel that there should be a more efficient matching scheme available as an alternative as many uses do not require this level of generality.

Contrast this with Condition (short form /;)

Reap[ StringCases["Mathematica", x__ /; Sow[x]] ][[2, 1]]

{"Mathematica", "Mathematic", "Mathemati", "Mathemat", "Mathema", "Mathem", 
"Mathe", "Math", "Mat", "Ma", "M", "athematica", "athematic", "athemati", 
"athemat", "athema", "athem", "athe", "ath", "at", "a", "thematica", "thematic", 
"themati", "themat", "thema", "them", "the", "th", "t", "hematica", "hematic", 
"hemati", "hemat", "hema", "hem", "he", "h", "ematica", "ematic", "emati", "emat", 
"ema", "em", "e", "matica", "matic", "mati", "mat", "ma", "m", "atica", "atic", 
"ati", "at", "a", "tica", "tic", "ti", "t", "ica", "ic", "i", "ca", "c", "a"}

Here we see that every possible alignment is tried, with the entire candidate sequence passed to the test function each time.