Recognize two letters followed by digit in filename (ISIN)

studio
string

#1

Hi All,
I do not find how to recognize an ISIN in a file name.
I would like to recognize a combination of letter and number for example “ABE-DE1585984-de-DE-26985-524” knowing that the numbers are random. and I just need “DE1585984” but sometimes this combination start the filename.

Do you have an idea?
Thanks for your help,
Quentin


#2

Hi Siobil,

Assuming letter and numbers combination always starts with “DE”?

Let’s take your array of filenames with Directory.GetFiles(path) and filter using .Where using your pattern with Regex.

You can do that like this:
regexPattern = "*DE[0-9]{7,8}*"
filenames = Directory.GetFiles(path).Where( Function(f) System.Text.RegularExpressions.Regex.IsMatch(f.ToString,regexPattern) ).ToArray

That will return a list of filenames that meet the pattern assigned in regexPattern. If there is only ever 1 filename then you can add (0) to the end to signify the first element in the array.

The regexPattern I used basically says to match if the string contains anything infront of “DE” followed by either 7 or 8 numbers. I’m not an expert on patterns but there could be adjustments needed depending on all your requirements.

EDIT: Also, to add, for testing your pattern you can output using WriteLine or Message Box like this:
System.Text.RegularExpressions.Regex.IsMatch(“ABE-DE1585984-de-DE-26985-524” ,regexPattern).ToString
will return True or False

Regards.

C


#3

Hi Clayton,
First thanks for your help.
I have tested but when I put this formula in matches activity I have this message “Quantifier{x,y} following nothing”.
Can I put the matches activity inside of my for each?

Thanks,
Quentin


#4

Hi,

I can’t say I’m a fan of the Match activities, but if you would like to use that you can show a screenshot of the activitiy and properties so the error can be analyzed.

To be honest, I would just use the vb.net to extract only the files that meet the pattern with .Where example I posted above. However, there is an alternative method by looping through each filename, which I can show a pseudocode below

regexPattern = "*DE[0-9]{7,8}*"
ForEach file In Directory.GetFiles(path)
with type argument as String
- If System.Text.RegularExpressions.Regex.IsMatch(Path.GetFileName(file.ToString),regexPattern)
— Process Sequence

As you can see, you can use Regex.IsMatch in the If condition and no need for an activity. If you would like to output the result of the condition then place it in a Message Box with .ToString on the end.

Regards.


#5

Hi Clayton,
I’m not sure to understand what I need to do to build this request. sorry

Thanks,
Quentin


#6

Sorry,
let me clarify.

The word “Path” in Path.GetFileName() should still be Path. So just change “MediaFolder” back to “Path” in your If Condition.

Also file.ToString inside that part should be whatever variable you use in your For Each. You have “item” in your For Each so that should be item.ToString

Let me know if that fixes the error.


#7

Hi,
Okay I did it btu when I run it I have this

image

but in my folder I have a file with this name "ID-DE1515"
Thanks


#8

Hi,
Looks like the wildcard * was throwing it off

Use a Pattern like this instead or something similar:
“DE[0-9]{3,10}”

So it’s any string containing DE followed by 3 to 10 numbers.

Regards.


#9

Hi Clayton,
It’s works !! :smile::smile::smile:
Thanks a lot !!

Best regards,
Quentin


#10

Hello @Siobil @ClaytonM, i am trying to do something very similar, is it possible to have the flow?
I have tried to follow the steps mentioned in this post but it does not do quite the same yet.

thank you!