cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
  • Due to the recently reported Microsoft outage, users may be temporarily unable to access My JMP or experience a delay receiving their verification codes. If you have previously installed JMP on your computer, this outage should not impact your ability to access the software. My JMP services will be restored as soon as Microsoft resolves this issue.
  • Image to Table JMP extension imports single or batch images as data tables. Useful to extract curves from pictures or convert image data to curve data to use in analyses like Functional Data Explorer. Download and install the extension.

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