cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
  • JMP 19 is here! See the new features at jmp.com/new.
  • Register to attend Discovery Summit 2025 Online: Early Users Edition, Sept. 24-25.
Choose Language Hide Translation Bar
robot
Level VI

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.
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

Recommended Articles