Taking a date value from a string

HI all,

If I’ve got a string which contains a date (in this example) such as

‘blah blah blah 20/04/2019 blah blah’, how do I extract the date and put it in it’s own variable?

It doesn’t have to be a date, it could be the word ‘lemons’ for example.

Thanks in advance
Jordan

1 Like

HI @jordrowley - You can use split string, in that you can split the text with " ", “/” whatever the way you want to split the text.

Regards,
AK

1 Like

Hi @jordrowley

Is that always the format of the string ?

cheers :smiley:

Happy learning :smiley:

I almost always recommend regular expressions for searching for and/or pulling out specific substrings within a string. It is generally a robust string manipulation solution

.NET Regex Reference - Regex Storm is a good vb.net regex cheat-sheet and tester

EDIT: it looks like gibberish, but this pattern would pull out a date in the format you specified, assuming it is preceded and followed by a space or line break: \b\d+/\d+/\d+\b

1 Like

Hey @pattyricarte

I’m working with a text file and the information I need is usually the middle of 3 lines of text i.e in the below example I need ‘20/04/2019’

Date from
20/04/2019
Other licence number

So to get the date here, I use an assign in to an array called ‘arrayparts’

MyTextFile.Split({“Date from”, “Other licence number”}, StringSplitOptions.None)

and then i’ll use another assign to get the 1st line from the array which will be the ‘middle value’

arrParts(1).Trim in to a string variable called ‘Datefrom’ for example and it will return 20/04/2019

However I get this example where when I follow the above logic later on in my text file

‘20/04/2019
I am covered by another TV Licence at the same address’

So I need to remove that bottom line of text, leaving just the date if that makes sense?

Thank you
Jordan

1 Like

Thanks Dave, I do sort of get that, you are looking for a digit and a ‘/’ with a blank space each side; reg ex is on my list of things to learn :slight_smile:

1 Like

Thanks for replying @AnandKumar26, I will also try your solution

1 Like

If it’s always 3 lines of text and you always want the middle text, then Strings.Split is definitely the way to go. I would split it on environment.newline though instead of hardcoding in the text. So it’d look something like this:

Assign YourTextArrayVariable = Strings.Split(MyTextFile,environment.newline,StringSplitOptions.None)

Assign OnlyStringYouWantVariable = YourTextArrayVariable(1)

This would always choose the 2nd line in MyTextFile