- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Report Inappropriate Content
How to return all matched results from Regex?
How can I return all results from a Regex search?
I am using JMP 18.1.2.
Names Default To Here( 1 );
string = "I am looking for ^this^, ^that^, and sometimes ^those^.";
result = Regex( string, "\^([^\n]*?)\^" ); // Only returns first match.
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Report Inappropriate Content
Re: How to return all matched results from Regex?
To my knowledge just using Regex (or Regex Match) are not able to do it at the moment, Add flag to Regex Match() to find all non-overlapping occurances of pattern . You could use Python but you can also use JSL with a loop
Names Default To Here(1);
string = "I am looking for ^this^, ^that^, and sometimes ^those^.";
result = Regex(string, "\^([^\n]*?)\^"); // Only returns first match.
matches = {};
While(!Is Missing(result = Regex(string, "\^([^\n]*?)\^")),
Substitute Into(string, result, "");
Insert Into(matches, result);
);
show(matches); // matches = {"^this^", "^that^", "^those^"};
or the much more complicated option of JMPs "own" pattern matching Scripting Guide > Types of Data > Pattern Matching
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Report Inappropriate Content
Re: How to return all matched results from Regex?
The Regex Match() function might work.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Report Inappropriate Content
Re: How to return all matched results from Regex?
Thanks for the input. Regex Match() still does not return all results. At least not the way I have written it.
Names Default To Here( 1 );
string = "I am looking for ^this^, ^that^, and sometimes ^those^.";
result = Regex( string, "\^([^\n]*?)\^" ); // Returns "^this^".
Show( result );
results = Regex Match( string, "\^([^\n]*?)\^" ); // Returns {"^this^", "this"}.
Show( results );
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Report Inappropriate Content
Re: How to return all matched results from Regex?
To my knowledge just using Regex (or Regex Match) are not able to do it at the moment, Add flag to Regex Match() to find all non-overlapping occurances of pattern . You could use Python but you can also use JSL with a loop
Names Default To Here(1);
string = "I am looking for ^this^, ^that^, and sometimes ^those^.";
result = Regex(string, "\^([^\n]*?)\^"); // Only returns first match.
matches = {};
While(!Is Missing(result = Regex(string, "\^([^\n]*?)\^")),
Substitute Into(string, result, "");
Insert Into(matches, result);
);
show(matches); // matches = {"^this^", "^that^", "^those^"};
or the much more complicated option of JMPs "own" pattern matching Scripting Guide > Types of Data > Pattern Matching
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Report Inappropriate Content
Re: How to return all matched results from Regex?
Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Report Inappropriate Content
Re: How to return all matched results from Regex?
Using JMP's Word() function along with Jarmo's methodology one can also solve the problem.
Names Default To Here(1);
string = "I am looking for ^this^, ^that^, and sometimes ^those^.";
result = Regex(string, "\^([^\n]*?)\^"); // Only returns first match.
matches = {};
While(!Is Missing(result = word(2,string, "^")),
Substitute Into(string, "^"||result||"^", "");
Insert Into(matches, result);
);
show(matches); // matches = {"^this^", "^that^", "^those^"};
matches = {"this", "that", "those"};