cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Register for our Discovery Summit 2024 conference, Oct. 21-24, where you’ll learn, connect, and be inspired.
Choose Language Hide Translation Bar
nikles
Level VI

How to make Regex case insensitive

Hi.  Does anyone know the best way to make a regex search case insensitive in JSL?  For example, I wish to extract the file extension from a path name, and I don't want it to matter if the extension is ".csv" or ".CSV" or ".cSv".  If I try the following, it will only work on filenames whose extensions are lowercase:

 

filename = Regex(path, "(.+\/)*([^\/]+)\.(csv|xlsx)$", "\2")

 

One method is to just convert the entire path to lowercase before doing regex on it, but for various reasons I don't want to do that.  

 

filename = Regex(LowerCase(path), "(.+\/)*([^\/]+)\.(csv|xlsx)$", "\2")

 

The other method I tried is to use the "(?i)" modifier in the match string, but I get an error:

filename = Regex(path, "(?i)(.+\/)*([^\/]+)\.(csv|xlsx)$", "\2")

"regex found a quantifier *, +, ?, or {  in an invalid position, the result is undefined in access or evaluation of 'Regex'".  I'm guessing that modifier is not supported in JSL.  Any ideas?

1 ACCEPTED SOLUTION

Accepted Solutions
jthi
Super User

Re: How to make Regex case insensitive

There is also IGNORECASE flag in Regex in JMP, but still I would suggest using other methods than regex if you just want the filename (or extension or both)

Names Default To Here(1);

path = Convert File Path("$SAMPLE_DATA/Big Class.jmp");
filename1 = Regex(path, "(.+\/)*([^\/]+)\.(JMP)$", "\2", IGNORECASE);
filename2 = Word(-2, path, "\/.");
filename3 = Word(-1, path, "\/");

Show(filename1, filename2, filename3);

jthi_0-1722668744463.png

Regex(source, pattern, (<replacement string>, <format, "GLOBALREPLACE", "IGNORECASE">>)

Scripting Guide > Types of Data > Regular Expressions > Regex Function 

-Jarmo

View solution in original post

4 REPLIES 4
jthi
Super User

Re: How to make Regex case insensitive

If you wish to extract file extension I would suggest checking out Word(), Words(), Item() and Items() functions instead of using Regex.

-Jarmo
jthi
Super User

Re: How to make Regex case insensitive

There is also IGNORECASE flag in Regex in JMP, but still I would suggest using other methods than regex if you just want the filename (or extension or both)

Names Default To Here(1);

path = Convert File Path("$SAMPLE_DATA/Big Class.jmp");
filename1 = Regex(path, "(.+\/)*([^\/]+)\.(JMP)$", "\2", IGNORECASE);
filename2 = Word(-2, path, "\/.");
filename3 = Word(-1, path, "\/");

Show(filename1, filename2, filename3);

jthi_0-1722668744463.png

Regex(source, pattern, (<replacement string>, <format, "GLOBALREPLACE", "IGNORECASE">>)

Scripting Guide > Types of Data > Regular Expressions > Regex Function 

-Jarmo
txnelson
Super User

Re: How to make Regex case insensitive

This hybrid code works 

Names Default To Here( 1 );
path = "C:\users\big.cSv";
filename = "";
ext = Word( -1, path, "." );
If( Uppercase( ext ) == "CSV" | Uppercase( ext ) == "XLSX",
	filename = Eval(
		Parse(
			"Regex(path, \!"(.+\/)*([^\/]+)\." || ext || "$\!", \!"\2\!")"
		)
	)
);
Show( filename );

My preferred JSL is

Names Default To Here( 1 );
path = "C:\users\big.cSv";
filename = "";
ext = Word( -1, path, "." );
If( Uppercase( ext ) == "CSV" | Uppercase( ext ) == "XLSX",
	filename = Word( -2, path, ".\/" )
);
Show( filename );
Jim
nikles
Level VI

Re: How to make Regex case insensitive

Thanks @jthi .  I knew I'd seen this somewhere.  Of course it was in the scripting guide.  Ha.