解
使用模式匹配功能
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”,“noon”,“強調甜點”,“Stanley Yelnats”,“看到Anna is”,“ere”};
position = {8,22,26,31,53,69,84};
討論
PatRepeat是一種模式匹配函數,只要它可以重複其模式。如果沒有PatLen(1)替代方法,它不能跳過不需要的文本,它允許它一次忽略一個字符,直到找到所需模式的另一個實例。
你也可以使用一個非常簡單的模式:
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 = {“是”,“是”};
position = {2,5};