How can I find the file name with specific characters

I have hundreds of text files with names like: D14 J4N4-BAPN_633nm_20x_100%_30accu_10s_point 1and D14 J4N5-NOBAPN_633nm_20x_100%_30accu_10s_point 3, as shown in the image below:

enter image description here

I would like to select files whose names contain BAPNas a group, and NOBAPN- as another group. But BAPNthey NOBAPNcontain the same characters as BAPN. How can i achieve this?

+4
source share
3 answers

The aughta simple regex does what you are looking for:

(BAPN)|(NOBAPN)

can be checked here - regex101 BAPN

This will capture these exact lines as separate captures without overlapping.

+5

, .

Select[{"D14 J4N4-BAPN_633nm_20x_100%_30accu_10s_point 1", 
  "D14 J4N5-NOBAPN_633nm_20x_100%_30accu_10s_point 3"},
 StringMatchQ[#, "*-BAPN*"] &]
{"D14 J4N4-BAPN_633nm_20x_100%_30accu_10s_point 1"}

Complement, .

stringlist = {
   "D14 J4N4-BAPN_633nm_20x_100%_30accu_10s_point 2",
   "D14 J4N4-BAPN_633nm_20x_100%_30accu_10s_point 1",
   "D14 J4N5-NOBAPN_633nm_20x_100%_30accu_10s_point 3",
   "D14 J4N5-NOBAPN_633nm_20x_100%_30accu_10s_point 2",
   "D14 J4N5-NOBAPN_633nm_20x_100%_30accu_10s_point 1"};

posnobapn = Position[stringlist , _?(StringContainsQ[#, "NOBAPN"] &), Heads -> False];
posbapn = Position[stringlist , _?(StringContainsQ[#, "BAPN"] &), Heads -> False];

listbapn = Extract[stringlist , Complement[posbapn, posnobapn]]
{"D14 J4N4-BAPN_633nm_20x_100%_30accu_10s_point 2",
 "D14 J4N4-BAPN_633nm_20x_100%_30accu_10s_point 1"}
listnobapn = Extract[stringlist , posnobapn]
{"D14 J4N5-NOBAPN_633nm_20x_100%_30accu_10s_point 3",
 "D14 J4N5-NOBAPN_633nm_20x_100%_30accu_10s_point 2",
 "D14 J4N5-NOBAPN_633nm_20x_100%_30accu_10s_point 1"}

, Nieminen.

regexcases = StringCases[stringlist, RegularExpression["(BAPN)|(NOBAPN)"]];

Pick[stringlist, regexcases /. {"BAPN"} -> True]
{"D14 J4N4-BAPN_633nm_20x_100%_30accu_10s_point 2",
 "D14 J4N4-BAPN_633nm_20x_100%_30accu_10s_point 1"}
Pick[stringlist, regexcases /. {"NOBAPN"} -> True]
{"D14 J4N5-NOBAPN_633nm_20x_100%_30accu_10s_point 3",
 "D14 J4N5-NOBAPN_633nm_20x_100%_30accu_10s_point 2",
 "D14 J4N5-NOBAPN_633nm_20x_100%_30accu_10s_point 1"}
+2

You can also use the dir command to sort the files you need.

To get file names using BAPN:

set1 = dir('*-BAPN*.txt') ;

To get file names using NOBAPN

set2 = dir('*-NOBAPN*.txt') ;

set1 and set2 will be structures.

0
source

Source: https://habr.com/ru/post/1678970/


All Articles