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!
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.
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.
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?
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" )