Finding a date in string


#1

Hello,

Is there a way how to find a date in a string? Lets say that I have "sjdajad fdadsad afa afa fffa sfsf 16.3.2017 q1e1dmq. dkwqk1e ".

Thanks a lot guys


#2

Hi,

Maybe try Regex, since you can use a pattern.

text = "sjdajad fdadsad afa afa fffa sfsf 16.3.2017 q1e1dmq. dkwqk1e "
text = System.Text.RegularExpressions.Regex.Match(text, “[0-9]{2}.[0-9]{1}.[0-9]{4}”).Value

I think that will work.
The “.” in the middle of the numbers can be used with “/” or any character, and the “0-9” is for number and {2} is how many digits.

Thanks.

EDIT: My function needs a slight adjustment cause it’s picking up the other numbers.
EDIT2: Did some testing and found the right function. I have updated the above with working Regex.


#3

Edited initial reply with correct Regex :slight_smile:


#4

Thanks a lot…its workin!!! Is it possible to apply this on letters? Lets say I am looking for Date: 16.07.2017 but there could be Date in different languages. It might problem the the lenght of the word is different isnt it?


#5

Ok, I’m not sure about the languages. You could add in “Date:” in the pattern like:
System.Text.RegularExpressions.Regex.Match(text, “Date: [0-9]{2}\.[0-9]{1}\.[0-9]{4}”).Value

Then, use an If statement to see if that = “” which means no text was found with that pattern.
If it doesn’t match, then change the word “Date:” with another word, so you could store it in an array.

dateTexts = { “Date:”,“lang1:”,“lang2:”,“lang3:” }

Then, place a For Loop with the Regex Assign inside, like as such:


For Each dateLang In dateTexts

Assign strDate = System.Text.RegularExpressions.Regex.Match(text, dateLang+“ [0-9]{2}\.[0-9]{1}\.[0-9]{4}”).Value

If strDate <> “” then
Break Loop


So in other words, you hardcode all the date words into an array, then loop through them til a match is found or the end of the loop.

EDIT: changed . to \. as sfranzen recommended


#6

Do you have some examples of the input you want to handle? You can make a regex pattern for almost anything you want, but it might be much too complex for what you need. It’s important to use as much context as you can. Your example “16.3.2017” is not language sensitive but is culture sensitive: some cultures might prefer to write 3/16/2017 or even 2017-3-16. This is problematic if you really don’t know and find a date like 8.7.2017: is it 8 July or 7 August? Quite a difference.

If you also want to match strings like “8th of July 2017” or “August 7, 2017” then this can also be done, but the more different types of notation and language you want to cover, the more complex it will become.


#7

Hi,

I have the following DT:

2(j ljekarna pablo - heinzelova heinzelova 28 tel. c1/467220-466
zu ljekarna pablo, pulac 4a oib: 23197705042
r1
račun br.:4558-122-9
03.03.2017

I am looking for Date on every row by this command: Date1=System.Text.RegularExpressions.Regex.Match(row.item(“Text”).ToString(), “[0-9]{2}.[0-9]{2}.[0-9]{4}”).Value

However, it will always find this number. 23197705042
Do you know why?


#8

Yes: the . in a regular expression matches any type of character. The first 10 characters of 23197705042 are a match because they contain the groups 23, 97 and 0504, “separated” by the characters 1 and 7. If you want to match only the full stop character itself you have to write this as \.. In fact in the text you give, the date will also match your pattern, but only the first match is returned by Regex.Match.


#9

Thank you for your answer. Is there also a way how to ask if a string contains a letter using regex?


#10

Hi,

Instead of [0-9] use [A-Z] for all uppers or [a-z] for lowers, and [A-Za-z] for any case.


#11

You can also choose to not use regex with IsNumeric()

Like,
IsNumeric(“abc123”) will return False
or
not IsNumeric(“abc123”) will return True