cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
  • Instantly extract effect sizes, F-ratios, and FDR-adjusted p-values from your models with the Calculate Effects Sizes extension, available now in the JMP Marketplace!
  • New to JMP? Join us Sept. 23-24 for the Early User Edition of Discovery Summit, tailor-made for new users. Register now for free!
  • Your voice matters! Tell us how you prefer to receive JMP updates, so we can tailor our communication to your needs. Take short survey.

Discussions

Solve problems, and share tips and tricks with other JMP users.
Choose Language Hide Translation Bar
miguello
Level VII

Replace ALL occurrences of a match in a string

Let's say I have a very long string, which essentially are just field names, comma separated. 

My problem, when trying to break it into separate fields, is that some names have comma inside them, like "{0,0}" or "{45,-45}" or "{90,90}".

In order to separate this long string into fields I need to replace that comma with something.

This is a small piece of that string: "Normal_0_{0,0}, Normal_90_{45,-45}" - so I need to replace only that comma that inside curly brackets.

So I wrote this:

matches = Regex (longLine, "(\{\d{1,2})(,)(-?\d{1,2}\})", "\1|\3");

It's supposed to replace all the commas inside the string. But in reality it only replaces the first comma in the string.

Why? And how can I replace ALL the matches?

 

Side question: In this code 

Word(1, "Normal_0_{0,0}, Normal_90_{45,-45}" ,  ", ")

it would split the string by EITHER "," OR " ". How can I split by COMBINATION of characters, specifically split by ", " - comma followed by space?

1 ACCEPTED SOLUTION

Accepted Solutions
miguello
Level VII

Re: Replace ALL occurrences of a match in a string

As usual, few minutes after posting I bumped into the answer in the documentation.

All I needed was GLOBALREPLACE:

matches = Regex (longLine, "(\{\d{1,2})(,)(-?\d{1,2}\})", "\1|\3", GLOBALREPLACE);

View solution in original post

2 REPLIES 2
miguello
Level VII

Re: Replace ALL occurrences of a match in a string

As usual, few minutes after posting I bumped into the answer in the documentation.

All I needed was GLOBALREPLACE:

matches = Regex (longLine, "(\{\d{1,2})(,)(-?\d{1,2}\})", "\1|\3", GLOBALREPLACE);
jthi
Super User

Re: Replace ALL occurrences of a match in a string

For your side question: You cannot easily split with many characters by using Word(s). You might be able to use Regex Match OR you could first replace all ", " (Substitute, Regex) with some special character and then split on that using Word(s).

-Jarmo

Recommended Articles