Problema
Encuentra las posiciones de todas las apariciones de un patrón dentro de una cadena. (Gracias @DonMcCormack )
Solución
Usa las funciones de comparación de patrones.
source = "this is a racecar and ere noon stressed desserts for Stanley Yelnats saw Anna was there";
palindrome = (Pat Regex( "[a-zA-Z][a-zA-Z ]{0,999}" )) >> firstpart + // one or more first letters
(Pat Regex( "[a-zA-Z ]{0,1}" )) + // optional middle letter
Expr( Reverse( firstpart ) ); // the tail
list = {}; // places to
positions = {}; // keep results
Pat Match(
source,
Pat Repeat( // repeat *is* a loop, within the source string
(Pat Pos() >> location + // capture current position in string
palindrome >> another + // capture the text of the palindrome match
Pat Test( // this function runs some JSL to see if the current position matches
Insert Into( list, another );// the JSL saves
Insert Into( positions, location );// two answers
1 // pattest returns 1(OK) because we are not actually testing anything...just wanted to run some JSL
)//
)
| // above adds to list, below skips forward a letter to try again
Pat Len( 1 ) // this bit is more important than it looks; it matches all the text that isn't interesting
)
);
Show( list, positions );
list = {"a racecar a", "ere", "mediodía", "postres estresados", "Stanley Yelnats", "vi que Anna estaba", "ere"};
posiciones = {8, 22, 26, 31, 53, 69, 84};
Discusión
PatRepeat es una función de coincidencia de patrones que repite su patrón todo el tiempo que pueda. No puede omitir el texto no deseado sin la alternativa PatLen (1) que le permite ignorar un carácter a la vez hasta que se encuentre otra instancia del patrón deseado.
Puedes usar un patrón muy simple también:
source = "this is a racecar and ere noon stressed desserts for Stanley Yelnats saw Anna was there";
reallysimple = "is";
list = {}; // places to
positions = {}; // keep results
Pat Match(
source,
Pat Repeat( // repeat *is* a loop, within the source string
(Pat Pos() >> location + // capture current position in string
reallysimple >> another + // capture the text of the match
Pat Test( // this function runs some JSL to see if the current position matches
Insert Into( list, another );// the JSL saves
Insert Into( positions, location );// two answers
1 // pattest returns 1(OK) because we are not actually testing anything...just wanted to run some JSL
)//
)
| // above adds to list, below skips forward a letter to try again
Pat Len( 1 ) // this bit is more important than it looks; it matches all the text that isn't interesting
)
);
Show( list, positions );
list = {"is", "is"};
posiciones = {2, 5};