JSL ChallengeI am trying to figure out a straight-forward and elegant way to search a text string for all occurrences of a given pattern and get their positions without using a loop. Munger will tell me how many there are, but not where they are. I can think of a few round about ways, but I feel like I'm missing something.
Re: JSL ChallengeSeems like a cookbook idea.
https://community.jmp.com/t5/JSL-Cookbook/Search-a-string-for-all-occurrences-of-a-pattern/ta-p/79761
Re: JSL ChallengeAh OK. Just wondering. I have had people ask a similar question but in terms of amino acids or bases within a column and I mentioned that BLAST and other such tools are a better place for this kind of question. I have had a few people looking for specific strings within a column of text (like comments for example). But the Text Explorer would now fill that gap for those types of questions (given stop words, etc)Re: JSL ChallengeHonestly, this is just a general question I thought about in the process of doing something similar. I know I've run across the problem in the past, but can remember the context. Similarly, I'd like to be able to list of text strings identifying those elements that contain a target string (without having to use a loop).
Re: JSL ChallengeHi Don,
For what problem are you looking to solve with this JSL challange. I hope not a genomics one. There is a tool called BLAST that people use for sequence finding that does something simliar with some fuzzy logic included as well as a scoring mechanism.
If just for text exploration, I am not quite sure on this. I am guessing it is like using a find command in Word, TextPad, PDF Viewer or Web Browser that searchs for a string in a corpus of text and tells you a location of what it found. IF so, why not use those tools instead of re-inventing the wheel with JSL? or using Text Explorer and force it to look for what you want?
Just trying to understand context.
Chris
Re: JSL Challenge
I just realized this won't work until you adjust the positions for the fact you changed a k-length string to a 1-length string. So, if the substring was more than 1 charcter, the positions must be adjusted. Hence the final "if" clause.
str = "acdljkdfjhcdlkjgiucdlekjrfcdcd";
substr = "cd";
pos = loc(words(substitute(str, substr, "#"), ""), "#" );
if (length(substr) > 1, pos += (0::(length(substr)-1)*(nrow(pos)-1)::(length(substr)-1))`);
Re: JSL Challenge**************** This needs adjustmentL: see my later reply *********************
If you can bank on some character NOT appearing in the string, you could do something like:
str = "abclkhsdfcdslkjscdlkjlkjcdkljcdcd";
loc(words(substitute(str, "cd", "#"), ""), "#" );
Re: JSL ChallengeI think that you might be able to fashion a solution based on this example from the Scripting Index:
Names Default To Here( 1 );
source = "believe";
// [aeiou] matches exactly one vowel
// .*? is a reluctant (vs greedy) match. try it without the ? to see the greedy behavior
// \1 is a back reference to the first ( group -- [aeiou] is inside the first ( group
matches = Regex Match(
source, // a variable allows updating some text
"([aeiou])(.*?)(\1)", // a regex with parens makes back references
">\2<" // the match is replaced by text that uses a back reference
);
Show( source, matches );
// results:
// source = "b>li<VE></VE>