cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
  • 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!
  • Use World Cup data to build models, explore spatial relationships, and create informative visualizations in JMP. Register. July 17, 2 pm US Eastern Time.
  • 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
MathStatChem
Level VII

How do you replace square brackets "[" "]" in a string with Regex?

I know that [ and ] are special characters in regular expressions, but I can't figure the right escape string to be able to treat them as a specific character in a pattern string.  

 

What I want to do, as an example, is convert "[1 1 1]" to "1 1 1".  

 

 

2 ACCEPTED SOLUTIONS

Accepted Solutions
Craige_Hales
Super User

Re: How do you replace square brackets "[" "]" in a string with Regex?

maybe Regex issue with escaped characters 

You are probably fighting JMP's escaping getting mixed up with regex escaping @paul_vezzetti . 

JMP strings make a special case of \[ and ]\ anywhere in a JMP string to turn off JMP's escaping.

try \!\[ and just use a bare ] . Regex should not need to escape the ] unless it is in a [character class].

regex("[1 2 3]","\!\[(.*)]","\1")

"1 2 3"

\!\ is JMP's escape to make a literal backslash. Regex then sees the \[ and behaves as expected.

Alternatively, you could use the \[ ... ]\ , but it creates worse problems if you actually need ]\ in the regex and seems less clear:

regex("[1 2 3]","\[\[(.*)]]\","\1")

I hope JMP gets a regex-quoted string to solve this double escaping issue.

 

Craige

View solution in original post

pmroz
Super User

Re: How do you replace square brackets "[" "]" in a string with Regex?

For something that simple can't you just use substitute?

a = "[1 1 1]";
b = substitute(a, "[", "", "]", "");
show(b);
"1 1 1"

View solution in original post

2 REPLIES 2
Craige_Hales
Super User

Re: How do you replace square brackets "[" "]" in a string with Regex?

maybe Regex issue with escaped characters 

You are probably fighting JMP's escaping getting mixed up with regex escaping @paul_vezzetti . 

JMP strings make a special case of \[ and ]\ anywhere in a JMP string to turn off JMP's escaping.

try \!\[ and just use a bare ] . Regex should not need to escape the ] unless it is in a [character class].

regex("[1 2 3]","\!\[(.*)]","\1")

"1 2 3"

\!\ is JMP's escape to make a literal backslash. Regex then sees the \[ and behaves as expected.

Alternatively, you could use the \[ ... ]\ , but it creates worse problems if you actually need ]\ in the regex and seems less clear:

regex("[1 2 3]","\[\[(.*)]]\","\1")

I hope JMP gets a regex-quoted string to solve this double escaping issue.

 

Craige
pmroz
Super User

Re: How do you replace square brackets "[" "]" in a string with Regex?

For something that simple can't you just use substitute?

a = "[1 1 1]";
b = substitute(a, "[", "", "]", "");
show(b);
"1 1 1"

Recommended Articles