How to extract a 4 digit number with unknown position from a string?

Hello All,

If I have a string called “XYZ Company 2014 Data Consolidation”/“2014 Data Consolidation - XYZ Company”/“Data Consolidation - XYZ Company - 2014”.

As you can see, the string has various formats for saying the exact same thing. One assumption is that “2014” is going to be the only number within the string.

Is there any way to extract this number without knowing the exact location of this number in the string?

Thanks for your help!

There are several ways to do it with regexes. In the examples below, I’m assuming your string above is in variable MyVar.

1.) Extract the first 4 digit number:
System.Text.RegularExpressions.Regex.Match(MyVar, "\d{4}").Value

2.) Extract the first 4 digit number that’s between 1900 and the current year.
a.) Get all 4 digit numbers in the string like this
System.Text.RegularExpressions.Regex.Matches(MyVar, "\d{4})
b.) Create a For Each loop iterating over type System.Text.RegularExpressions.Match c.) In the loop, assuming the item in the For Each activity is set to Item, create an If activity with condition CInt(Item.Value) >= 1900 AndAlso CInt(Item.Value) <= CInt(Now.ToString(yyyy))`
d.) If the condition is met, extract the value in a separate value, and use a Break activity to leave the loop.

There are other ways of going about it, but the complexity will depend on how complex your string can be.


Thank you! It worked!

@ianhyj Please mark Anthony_Humphries post as the Answer