Search for string permutations


I’m looking for the best way to search for a substring that contains information whether the image is the surface or cross section of a sample. I’m using an “if” statement to key off any permutation of cross section since older files have multiple wordings.
General title looks like “smp-1-Xsec-area 200x.tif”, but cross section could be: Xsec, xsec, X sec, x sec, Cross, cross.
Looking through the forums it looks like using “matches” may be the best approach. However, regex is still not coming natural to me, but I’ve cobbled together some code that will work for at least two of the cases (Xsec, xsec). But there will be cases of X sec and x sec. How do I handle the space in the text using regex?

function analysis(img_filename,fil_num) {
		// stripping off extensions to start with base file name //
		saveAs("tiff", dir2 + baset + "_a" + fil_num); 								// saving cropped image
		if (matches(title, ".*Xs.*|.*xs.*") ) { 
         	pers = "Xsec"; 
		 	} else {
			pers = "surf";			


I’m not an expert on regex, but you can include spaces. This simple example seems to work for any of the four combinations you suggest:

myString="smp-1-X sec-area 200x.tif";

if (matches(myString, ".*(xsec|Xsec|X sec|x sec)+.*")){
	print("match"); } else {

Note that the + indicates at least one match of any of the four strings in the parentheses. Another option would be to include the space as a separete block using the asterisk to indicate zero or greater occurances (which also works in my limited testing):

matches(myString, ".*(x|X)+( )*sec.*")

It’s not going to win regex golf, but hopefully it’s helpful!

1 Like


I think it’s a pretty good “putt”! Outstanding!

Much cleaner and concise and it works for all the cases (I’ve found so far:)

Many thanks for your valuable time.


if (matches(title, ".*(Xs|xs|X s|x s|Cros|cros).*" ) ) { 		
         	pers = "Xsec"; 	
		 	} else {
			pers = "surface";			
1 Like