cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Choose Language Hide Translation Bar
HSS
HSS
Level IV

Regex help -

hello, I need just some help to use Regex function --

String is like --

"Fine.2020.Optional.(Not)"

 

Required -- "Fine_2020_Optional"

 

I have tried couple of combination, but not able to get the result in one regex. I am using this type of Regex in my for loop script.

 

Any help ?

 

Thanks.

 

 

1 ACCEPTED SOLUTION

Accepted Solutions

Re: Regex help -

It would probably help if we had more examples of input and expected output and where you are getting hung up. The JSL below works for your single case, but I'm not sure if it's applicable to all of your data in general.

 

Regex("Fine.2020.Optional.(Not)",
	"(\w+?)\.(\d+?)\.(\w+?)\.\(\w+?\)",
	"\1_\2_\3"
);

View solution in original post

11 REPLIES 11

Re: Regex help -

It would probably help if we had more examples of input and expected output and where you are getting hung up. The JSL below works for your single case, but I'm not sure if it's applicable to all of your data in general.

 

Regex("Fine.2020.Optional.(Not)",
	"(\w+?)\.(\d+?)\.(\w+?)\.\(\w+?\)",
	"\1_\2_\3"
);
HSS
HSS
Level IV

Re: Regex help -

Thanks Paul,

There few simpler one, I was able to do it but this was the one. which gave me some trouble. Next time if I found any issue, I will get back to you with more details. Do you have some reference / link to share with me /us on Regex with good examples ? That would be very helpful.

Thanks again.

Re: Regex help -

This website has some great general information about Regex:

 

https://www.regular-expressions.info/quickstart.html

 

There is also a tool for generating a regex based on your inputs and expected outputs:

 

http://regex.inginf.units.it/

HSS
HSS
Level IV

Re: Regex help -

This is really very helpful. Thanks.
Byron_JMP
Staff

Re: Regex help -

Regex( "this.is.jmp", "\.", "_", GLOBALREPLACE );

JMP Systems Engineer, Health and Life Sciences (Pharma)
Byron_JMP
Staff

Re: Regex help -

OK, so off line I got a couple of comments about the very short solution.  

Here's how to get there on you own. It might be cheating, but it's soooooo much easier.

 

I made a table with the example in it. Selected the column and used recode.

In Recode I used replace string, and turned on the regular expressions.

In the recode dialog I turned on both scripting options (bottom right)

Then saved the script to the script window, copied the little bit and pasted it here.  (well, verified it worked and then pasted it.)

 

Huge thanks to @ErnestPasour  : ) 

 

Screen Shot 2020-02-14 at 9.19.49 AM.png

 

Screen Shot 2020-02-14 at 9.20.09 AM.png

 

oh, and there is that little bit using "\." rather than "." alone. The backslash is an escape character, and keeps from replacing everything with "_".   That's the only bit of regex knowledge needed to make this work.

JMP Systems Engineer, Health and Life Sciences (Pharma)
WebDesignesCrow
Super User

Re: Regex help -

Thanks. This is very helpful
Jeff_Perkinson
Community Manager Community Manager

Re: Regex help -

Don't forget the built-in JMP functions for doing string manipulation, like Substitute(), Munger() and, my favorite, Word(). The syntax for those might be easier to deal with for something like this. 

 

str="Fine.2020.Optional.(Not)";

show(substitute(str, ".", "_"));

show(word(1, str, ".") || "_" || word(2, str, ".") || "_" || word(3, str, "."));

show(concat items(words(str, ".")[1::3], "_"));

Log:

Substitute(str, ".", "_") = "Fine_2020_Optional_(Not)";
Word(1, str, ".") || "_" || Word(2, str, ".") || "_" || Word(3, str, ".") = "Fine_2020_Optional";
Concat Items(Words(str, ".")[Index(1, 3)], "_") = "Fine_2020_Optional";

 

-Jeff
Byron_JMP
Staff

Re: Regex help -

Pattern match, could also be a good solution, but I keep getting tripped up on the syntax. If only there was someone around who was fluent in snbol...  @Craige_Hales 

JMP Systems Engineer, Health and Life Sciences (Pharma)