cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Try the Materials Informatics Toolkit, which is designed to easily handle SMILES data. This and other helpful add-ins are available in the JMP® Marketplace
Choose Language Hide Translation Bar
robust1972
Level IV

how to use jsl to test if a string contains only A~Z and 0~9?

how to use jsl to test if a string contains only A~Z and 0~9?

my data shall  contains only A~Z and 0~9 but when 1 or few bit flip then there is not printable characters.

many thanks in advance!

2 ACCEPTED SOLUTIONS

Accepted Solutions
pmroz
Super User

Re: how to use jsl to test if a string contains only A~Z and 0~9?

You can use regular expressions to check this.  Create a new column that contains the following formula:

If (Regex(:Name("2nd"), "^[a-zA-Z0-9]+$") == :Name("2nd"), "Yes", "No")


The new column will have the value "Yes" if it is purely alphanumeric, "No" otherwise.

View solution in original post

pmroz
Super User

Re: how to use jsl to test if a string contains only A~Z and 0~9?

The ^ means the start of the string, the + means to match at least one character, and the $ means the end of the string.

To return a 1 or 0 simply change Yes to 1 and No to 0:

If (Regex(:Name("2nd"), "^[a-zA-Z0-9]+$") == :Name("2nd"), 1, 0)

There are a ton of websites out there that explain regular expressions btw.

View solution in original post

7 REPLIES 7
pmroz
Super User

Re: how to use jsl to test if a string contains only A~Z and 0~9?

You can use regular expressions to check this.  Create a new column that contains the following formula:

If (Regex(:Name("2nd"), "^[a-zA-Z0-9]+$") == :Name("2nd"), "Yes", "No")


The new column will have the value "Yes" if it is purely alphanumeric, "No" otherwise.

robust1972
Level IV

Re: how to use jsl to test if a string contains only A~Z and 0~9?

thanks for your reply, PMroz.

What ^ and +$ for?

how can I make it return 1 or 0 so that I could use it together with other comparison algorithm?

pmroz
Super User

Re: how to use jsl to test if a string contains only A~Z and 0~9?

The ^ means the start of the string, the + means to match at least one character, and the $ means the end of the string.

To return a 1 or 0 simply change Yes to 1 and No to 0:

If (Regex(:Name("2nd"), "^[a-zA-Z0-9]+$") == :Name("2nd"), 1, 0)

There are a ton of websites out there that explain regular expressions btw.

ron_horne
Super User (Alumni)

Re: how to use jsl to test if a string contains only A~Z and 0~9?

Thank you@pmroz for the instructions. Very useful with some particularly dirty data.

If anyone else needs a handy example try the following:

 

If(
	Regex( :"2nd"n, "^[A-Z]+$" ) == :"2nd"n, "Uppercase",
	Regex( :"2nd"n, "^[a-z]+$" ) == :"2nd"n, "Lowercase",
	Regex( :"2nd"n, "^[0-9]+$" ) == :"2nd"n, "Numbers",
	Regex( :"2nd"n, "^[A-Z0-9]+$" ) == :"2nd"n, "Uppercase and numbers",
	Regex( :"2nd"n, "^[a-z0-9]+$" ) == :"2nd"n, "lowercase and numbers",
	Regex( :"2nd"n, "^[A-Za-z]+$" ) == :"2nd"n, "No numbers",
	Regex( :"2nd"n, "^[A-Za-z0-9]+$" ) == :"2nd"n, "All characters",
	Regex( :"2nd"n, "^[A-Za-z0-9]+$" ) != :"2nd"n,
		"includes non alphanumeric characters"
)

ron_horne_0-1669302873804.png

 

dadawasozo
Level IV

Re: how to use jsl to test if a string contains only A~Z and 0~9?

Hi 

I wonder if regex can be used in text? 

Text = " today I m at FL12, tomorrow I will be at FL102"

 

 I have rows of text that I want to find out if each row contains FL + anydigit then yes, or print the FL+digit. In this case, it will be FL12 and FL103.

If nor Regex, can you suggest other function?

 

jthi
Super User

Re: how to use jsl to test if a string contains only A~Z and 0~9?

If you only need to know found or not found, it is fairly simple with one regex, if you also need the matches you need to loop over the string while substituting the found matches. Also if you need to test your Regex I suggest regex101

 

Names Default To Here(1);

original_txt1 = " today I m at FL12, tomorrow I will be at FL 103";
original_txt2 = " today I m at FLb12, tomorrow I will be at FL a123";

// if you only need to know if there is a match combine regex with !IsMissing()
found1 = !Is Missing(Regex(original_txt1, "FL\s?\d+"));
found2 = !Is Missing(Regex(original_txt2, "FL\s?\d+"));
show(found1, found2);

// if you also need the matches loop over the string
matches = {};
While(!Is Missing(rgx_match = Regex(original_txt1, "FL\s?\d+")),
	Insert Into(matches, rgx_match);
	Substitute Into(original_txt1, rgx_match, "");
);
show(matches);

To my knowledge JMP cannot perform "global" regex search with Regex Match() and I have made wish list item for it Add flag to Regex Match() to find all non-overlapping occurances of pattern ). You could most likely use JMP's own Pattern Matching to do tasks like finding multiple matches, but I haven't bothered to learn it. Here is good post about thatPattern Matching 

 

-Jarmo
dadawasozo
Level IV

Re: how to use jsl to test if a string contains only A~Z and 0~9?

Thanks! The regex101 helps to understand the code you shared.