cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
%3CLINGO-SUB%20id%3D%22lingo-sub-79761%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%E3%83%91%E3%82%BF%E3%83%BC%E3%83%B3%E3%81%AE%E3%81%99%E3%81%B9%E3%81%A6%E3%81%AE%E5%87%BA%E7%8F%BE%E7%AE%87%E6%89%80%E3%81%AB%E3%81%A4%E3%81%84%E3%81%A6%E3%82%B9%E3%83%88%E3%83%AA%E3%83%B3%E3%82%B0%E3%82%92%E6%A4%9C%E7%B4%A2%E3%81%97%E3%81%BE%E3%81%99%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-79761%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%3CDIV%20class%3D%22lia-message-template-problem-zone%22%3E%0A%3CH2%20id%3D%22toc-hId-1624414659%22%20id%3D%22toc-hId-1617921153%22%20id%3D%22toc-hId-1617921153%22%3E%E5%95%8F%E9%A1%8C%3C%2FH2%3E%0A%3CP%3E%E6%96%87%E5%AD%97%E5%88%97%E5%86%85%E3%81%AE%E3%83%91%E3%82%BF%E3%83%BC%E3%83%B3%E3%81%AE%E3%81%99%E3%81%B9%E3%81%A6%E3%81%AE%E5%87%BA%E7%8F%BE%E4%BD%8D%E7%BD%AE%E3%82%92%E8%A6%8B%E3%81%A4%E3%81%91%E3%81%BE%E3%81%99%E3%80%82%EF%BC%88%E3%81%82%E3%82%8A%E3%81%8C%E3%81%A8%E3%81%86%3CA%20href%3D%22https%3A%2F%2Fcommunity.jmp.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F5080%22%20target%3D%22_blank%22%3E%20%40DonMcCormack%20%3C%2FA%3E%20%EF%BC%89%3C%2FP%3E%0A%3C%2FDIV%3E%0A%3CDIV%20class%3D%22lia-message-template-diagnosis-zone%22%3E%0A%3CH2%20id%3D%22toc-hId--927742302%22%20id%3D%22toc-hId--934235808%22%20id%3D%22toc-hId--934235808%22%3E%E6%BA%B6%E6%B6%B2%3C%2FH2%3E%0A%3CP%3E%E3%83%91%E3%82%BF%E3%83%BC%E3%83%B3%E3%83%9E%E3%83%83%E3%83%81%E3%83%B3%E3%82%B0%E6%A9%9F%E8%83%BD%E3%82%92%E4%BD%BF%E3%81%86%3C%2FP%3E%0A%3CPRE%3E%3CCODE%20class%3D%22%20language-jsl%22%3Esource%20%3D%20%22this%20is%20a%20racecar%20and%20ere%20noon%20stressed%20desserts%20for%20Stanley%20Yelnats%20saw%20Anna%20was%20there%22%3B%0A%0Apalindrome%20%3D%20(Pat%20Regex(%20%22%5Ba-zA-Z%5D%5Ba-zA-Z%20%5D%7B0%2C999%7D%22%20))%20%26gt%3B%26gt%3B%20firstpart%20%2B%20%2F%2F%20one%20or%20more%20first%20letters%0A(Pat%20Regex(%20%22%5Ba-zA-Z%20%5D%7B0%2C1%7D%22%20))%20%2B%20%2F%2F%20optional%20middle%20letter%0AExpr(%20Reverse(%20firstpart%20)%20)%3B%20%2F%2F%20the%20tail%0A%0Alist%20%3D%20%7B%7D%3B%20%2F%2F%20places%20to%0Apositions%20%3D%20%7B%7D%3B%20%2F%2F%20keep%20results%0APat%20Match(%0A%20%20%20%20source%2C%0A%20%20%20%20Pat%20Repeat(%20%2F%2F%20repeat%20*is*%20a%20loop%2C%20within%20the%20source%20string%0A%20%20%20%20%20%20%20%20(Pat%20Pos()%20%26gt%3B%26gt%3B%20location%20%2B%20%2F%2F%20capture%20current%20position%20in%20string%0A%20%20%20%20%20%20%20%20%20%20%20%20palindrome%20%26gt%3B%26gt%3B%20another%20%2B%20%2F%2F%20capture%20the%20text%20of%20the%20palindrome%20match%0A%20%20%20%20%20%20%20%20%20%20%20%20Pat%20Test(%20%2F%2F%20this%20function%20runs%20some%20JSL%20to%20see%20if%20the%20current%20position%20matches%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Insert%20Into(%20list%2C%20another%20)%3B%2F%2F%20the%20JSL%20saves%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Insert%20Into(%20positions%2C%20location%20)%3B%2F%2F%20two%20answers%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%201%20%2F%2F%20pattest%20returns%201(OK)%20because%20we%20are%20not%20actually%20testing%20anything...just%20wanted%20to%20run%20some%20JSL%0A%20%20%20%20%20%20%20%20%20%20%20%20)%2F%2F%0A%20%20%20%20%20%20%20%20)%20%0A%20%20%20%20%20%20%20%20%7C%20%2F%2F%20above%20adds%20to%20list%2C%20below%20skips%20forward%20a%20letter%20to%20try%20again%20%0A%20%20%20%20%20%20%20%20Pat%20Len(%201%20)%20%2F%2F%20this%20bit%20is%20more%20important%20than%20it%20looks%3B%20it%20matches%20all%20the%20text%20that%20isn't%20interesting%0A%20%20%20%20)%0A)%3B%0AShow(%20list%2C%20positions%20)%3B%3C%2FCODE%3E%3C%2FPRE%3E%0A%3C%2FDIV%3E%0A%3CDIV%20class%3D%22lia-message-template-diagnosis-zone%22%3E%3CSTRONG%3E%20list%20%3D%20%7B%22%E3%83%AC%E3%83%BC%E3%82%B9%E3%82%AB%E3%83%BCa%22%E3%80%81%22ere%22%E3%80%81%22%E6%AD%A3%E5%8D%88%22%E3%80%81%22%E3%82%B9%E3%83%88%E3%83%AC%E3%82%B9%E3%81%AE%E5%A4%9A%E3%81%84%E3%83%87%E3%82%B6%E3%83%BC%E3%83%88%22%E3%80%81%22Stanley%20Yelnats%22%E3%80%81%22Anna%E3%81%8C%E8%A6%8B%E3%81%9F%22%E3%80%81%22ere%22%7D%3B%20%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CSTRONG%3E%E4%BD%8D%E7%BD%AE%EF%BC%9D%20%EF%BD%9B%EF%BC%98%E3%80%81%EF%BC%92%EF%BC%92%E3%80%81%EF%BC%92%EF%BC%96%E3%80%81%EF%BC%93%EF%BC%91%E3%80%81%EF%BC%95%EF%BC%93%E3%80%81%EF%BC%96%EF%BC%99%E3%80%81%EF%BC%98%EF%BC%94%EF%BD%9D%E3%80%82%3C%2FSTRONG%3E%3C%2FDIV%3E%0A%3CDIV%20class%3D%22lia-message-template-solution-zone%22%3E%0A%3CH2%20id%3D%22toc-hId-815068033%22%20id%3D%22toc-hId-808574527%22%20id%3D%22toc-hId-808574527%22%3E%E8%A8%8E%E8%AB%96%3C%2FH2%3E%0A%3CP%3EPatRepeat%E3%81%AF%E3%80%81%E5%8F%AF%E8%83%BD%E3%81%AA%E9%99%90%E3%82%8A%E3%83%91%E3%82%BF%E3%83%BC%E3%83%B3%E3%82%92%E7%B9%B0%E3%82%8A%E8%BF%94%E3%81%99%E3%83%91%E3%82%BF%E3%83%BC%E3%83%B3%E3%83%9E%E3%83%83%E3%83%81%E3%83%B3%E3%82%B0%E6%A9%9F%E8%83%BD%E3%81%A7%E3%81%99%E3%80%82%E7%9B%AE%E7%9A%84%E3%81%AE%E3%83%91%E3%82%BF%E3%83%BC%E3%83%B3%E3%81%AE%E5%88%A5%E3%81%AE%E3%82%A4%E3%83%B3%E3%82%B9%E3%82%BF%E3%83%B3%E3%82%B9%E3%81%8C%E8%A6%8B%E3%81%A4%E3%81%8B%E3%82%8B%E3%81%BE%E3%81%A7%E4%B8%80%E5%BA%A6%E3%81%AB1%E6%96%87%E5%AD%97%E3%82%92%E7%84%A1%E8%A6%96%E3%81%A7%E3%81%8D%E3%82%8BPatLen%EF%BC%881%EF%BC%89%E3%81%AE%E4%BB%A3%E6%9B%BF%E6%89%8B%E6%AE%B5%E3%81%8C%E3%81%AA%E3%81%84%E3%81%A8%E3%80%81%E4%B8%8D%E8%A6%81%E3%81%AA%E3%83%86%E3%82%AD%E3%82%B9%E3%83%88%E3%82%92%E3%82%B9%E3%82%AD%E3%83%83%E3%83%97%E3%81%99%E3%82%8B%E3%81%93%E3%81%A8%E3%81%AF%E3%81%A7%E3%81%8D%E3%81%BE%E3%81%9B%E3%82%93%E3%80%82%3C%2FP%3E%0A%3CP%3E%E6%9C%AC%E5%BD%93%E3%81%AB%E7%B0%A1%E5%8D%98%E3%81%AA%E3%83%91%E3%82%BF%E3%83%BC%E3%83%B3%E3%82%82%E4%BD%BF%E3%81%88%E3%81%BE%E3%81%99%E3%80%82%3C%2FP%3E%0A%3CPRE%3E%3CCODE%20class%3D%22%20language-jsl%22%3Esource%20%3D%20%22this%20is%20a%20racecar%20and%20ere%20noon%20stressed%20desserts%20for%20Stanley%20Yelnats%20saw%20Anna%20was%20there%22%3B%0A%0Areallysimple%20%3D%20%22is%22%3B%20%0A%0Alist%20%3D%20%7B%7D%3B%20%2F%2F%20places%20to%0Apositions%20%3D%20%7B%7D%3B%20%2F%2F%20keep%20results%0APat%20Match(%0A%20%20%20%20source%2C%0A%20%20%20%20Pat%20Repeat(%20%2F%2F%20repeat%20*is*%20a%20loop%2C%20within%20the%20source%20string%0A%20%20%20%20%20%20%20%20(Pat%20Pos()%20%26gt%3B%26gt%3B%20location%20%2B%20%2F%2F%20capture%20current%20position%20in%20string%0A%20%20%20%20%20%20%20%20%20%20%20%20reallysimple%20%26gt%3B%26gt%3B%20another%20%2B%20%2F%2F%20capture%20the%20text%20of%20the%20match%0A%20%20%20%20%20%20%20%20%20%20%20%20Pat%20Test(%20%2F%2F%20this%20function%20runs%20some%20JSL%20to%20see%20if%20the%20current%20position%20matches%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Insert%20Into(%20list%2C%20another%20)%3B%2F%2F%20the%20JSL%20saves%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Insert%20Into(%20positions%2C%20location%20)%3B%2F%2F%20two%20answers%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%201%20%2F%2F%20pattest%20returns%201(OK)%20because%20we%20are%20not%20actually%20testing%20anything...just%20wanted%20to%20run%20some%20JSL%0A%20%20%20%20%20%20%20%20%20%20%20%20)%2F%2F%0A%20%20%20%20%20%20%20%20)%20%0A%20%20%20%20%20%20%20%20%7C%20%2F%2F%20above%20adds%20to%20list%2C%20below%20skips%20forward%20a%20letter%20to%20try%20again%20%0A%20%20%20%20%20%20%20%20Pat%20Len(%201%20)%20%2F%2F%20this%20bit%20is%20more%20important%20than%20it%20looks%3B%20it%20matches%20all%20the%20text%20that%20isn't%20interesting%0A%20%20%20%20)%0A)%3B%0AShow(%20list%2C%20positions%20)%3B%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%3CSTRONG%3E%20list%20%3D%20%7B%22is%22%E3%80%81%22is%22%7D%3B%20%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CSTRONG%3E%E4%BD%8D%E7%BD%AE%3D%20%7B2%E3%80%815%7D%E3%80%82%3C%2FSTRONG%3E%3C%2FP%3E%0A%3C%2FDIV%3E%0A%3CDIV%20class%3D%22lia-message-template-solution-zone%22%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CDIV%20class%3D%22lia-message-template-solution-zone%22%3EPatPos%E9%96%A2%E6%95%B0%E3%81%AF%E3%80%81%E3%83%9E%E3%83%83%E3%83%81%E3%83%A3%E3%83%BC%E3%81%AE%E3%82%AB%E3%83%BC%E3%82%BD%E3%83%AB%E4%BD%8D%E7%BD%AE%EF%BC%88%E6%9C%80%E5%88%9D%E3%81%AE%E6%96%87%E5%AD%97%E3%81%AE%E5%89%8D%E3%81%AA%E3%81%A9%E3%81%AF%E3%82%BC%E3%83%AD%EF%BC%89%E3%82%92%E5%8F%96%E5%BE%97%E3%81%99%E3%82%8B%E3%81%9F%E3%82%81%E3%81%AB%E5%BC%95%E6%95%B0%E3%81%AA%E3%81%97%E3%81%A7%E4%BD%BF%E7%94%A8%E3%81%95%E3%82%8C%E3%82%8B%E3%81%93%E3%81%A8%E3%81%AB%E6%B3%A8%E6%84%8F%E3%81%97%E3%81%A6%E3%81%8F%E3%81%A0%E3%81%95%E3%81%84%E3%80%82PatPos%EF%BC%88N%EF%BC%89%E3%81%AF%E3%80%81%E3%82%AB%E3%83%BC%E3%82%BD%E3%83%AB%E3%81%8CN%E3%81%AE%E4%BD%8D%E7%BD%AE%E3%81%AB%E3%81%82%E3%82%8B%E5%A0%B4%E5%90%88%E3%81%AB%E6%88%90%E5%8A%9F%E3%81%99%E3%82%8B%E3%83%86%E3%82%B9%E3%83%88%E3%81%A7%E3%81%99%E3%80%82%3CBR%20%2F%3E%0A%3CH2%20id%3D%22toc-hId--1737088928%22%20id%3D%22toc-hId--1743582434%22%20id%3D%22toc-hId--1743582434%22%3E%E5%8F%82%E7%85%A7%3C%2FH2%3E%0A%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fcommunity.jmp.com%2Ft5%2FUncharted%2FPattern-Matching%2Fba-p%2F21005%22%20target%3D%22_blank%22%3Ehttps%3A%2F%2Fcommunity.jmp.com%2Ft5%2FUncharted%2FPattern-Matching%2Fba-p%2F21005%3C%2FA%3E%3C%2FP%3E%0A%3CA%20href%3D%22https%3A%2F%2Fwww.jmp.com%2Fsupport%2Fhelp%2F14%2Fpattern-matching.shtml%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ehttps%3A%2F%2Fwww.jmp.com%2Fsupport%2Fhelp%2F14%2Fpattern-matching.shtml%3C%2FA%3E%3C%2FDIV%3E%3C%2FLINGO-BODY%3E
Choose Language Hide Translation Bar
Search a string for all occurrences of a pattern

Problem

Find the positions of all occurrences of a pattern within a string. (Thanks @DonMcCormack )

Solution

Use the pattern matching functions

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", "stressed desserts", "Stanley Yelnats", "saw Anna was", "ere"};
positions = {8, 22, 26, 31, 53, 69, 84};

Discussion

PatRepeat is a pattern matching function that repeats its pattern as long as it can. It can't skip over the undesired text without the PatLen(1) alternative that lets it ignore one character at a time until another instance of the desired pattern is found.

You can use a really simple pattern too:

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"};
positions = {2, 5};

 
Note that the PatPos function is used without an argument to get the matcher's cursor position (which is zero before the first character, etc). PatPos(N) is a test that succeeds if the cursor is at N.

See Also

https://community.jmp.com/t5/Uncharted/Pattern-Matching/ba-p/21005

https://www.jmp.com/support/help/14/pattern-matching.shtml
JSL Cookbook

If you’re looking for a code snippet or design pattern that performs a common task for your JSL project, the JSL Cookbook is for you.

This knowledge base contains building blocks of JSL code that you can use to reduce the amount of coding you have to do yourself.

It's also a great place to learn from the experts how to use JSL in new ways, with best practices.