cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
The Discovery Summit 2025 Call for Content is open! Submit an abstract today to present at our premier analytics conference.
See how to use to use Text Explorer to glean valuable information from text data at April 25 webinar.
Choose Language Hide Translation Bar
View Original Published Thread

How to return all matched results from Regex?

robot
Level VI

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.
1 ACCEPTED SOLUTION

Accepted Solutions
jthi
Super User


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 

 

-Jarmo

View solution in original post

5 REPLIES 5


Re: How to return all matched results from Regex?

The Regex Match() function might work.

robot
Level VI


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 );
jthi
Super User


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 

 

-Jarmo
robot
Level VI


Re: How to return all matched results from Regex?

Thanks!

txnelson
Super User


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"};
Jim