cancel
Showing results for 
Search instead for 
Did you mean: 

How to iterate date in text file using regular expression

Contributor

How to iterate date in text file using regular expression

I am trying to iterate the date inside the text file and want to import the file in my application using test complete,

 

tried using regular expression its not working 

 

For ex: 

30/05/2018,09:30,00:25,Slot2,NH New History-2,03,,07,,Comedy,16,,06,,"Host has to offer.",2016,,,ENG,SGGTY5527,,,25,,,,,,,,,,,,1|2|3,30,VOD,E,,,Y
30/05/2018,11:35,00:25,Slot1,NH New History-1,03,,07,,Comedy,18,,F,,"Host has to offer.",2016,,,ENG,SGGYT5127,,,25,,,,,,,,,,,,1|2|3,15,VOD,M,,,N
30/05/2018,14:30,00:25,Slot3,NH New History-3,03,,07,,Comedy,16,,06,,"Host has to offer.",2016,,,ENG,SGYGT5627,,,25,,,,,,,,,,,,1|2|3,30,VOD,,,,

 

in the above text the date should get replaced by 30/05/2019 for all occurrences .

 

The code i tried :

 s = "Some 20/04/2021 ex 2021/02/21 dfsdkjfdksajsda"     

 

var re =/^(0?[1-9]|[12][0-9]|3[01])[\/](0?[1-9]|1[012])[\/\-]\d{4}$/g;

var m;

while (m = re.exec(s)) {

 

var myDate=new Date(m)

myDate.setFullYear(myDate.getFullYear() + 1);

a=s.replace(m,myDate.toLocaleDateString())

  alert(a);

}

       

 

5 REPLIES 5
Community Hero

Re: How to iterate date in text file using regular expression

What are you trying to do in your test with this iterated date?  Perhaps we can come up with an easier way to do it.

Contributor

Re: How to iterate date in text file using regular expression

@Marsha_R

 

Thanks!

 

I want to import the file for my test so changing the dates every time before import .

 

example : if the file has the date : 02/05/2018 , next time it should get incremented by year like 02/05/2019

 

Please suggest

Community Hero

Re: How to iterate date in text file using regular expression

Is the date the same for every line in the file?  Perhaps you could use a project variable and read the value from that instead.  It would be a lot easier to increment it there instead of trying to update all those strings.

Contributor

Re: How to iterate date in text file using regular expression

Yes Year is same for every line in the file , so if i try  project variable and increment year each time but need to change year for the first time in variable initialisation because file changes during my testing .

 

Steps to follow:

 

if i get the input file , first need to increment the year inside the file and then will import into test application.

 

 

Frequent Contributor

Re: How to iterate date in text file using regular expression

Hi,

 

As @Marsha_R said there are multiple ways to accomplish the task. If you are looking for regular expression here is a sample script which is being written in vbscript to fetch only the Date value from a string increment it and replace with the incremented value.

 

Sub Test()
 oriDate = GetDateFromString("27/02/1988,09:30,00:25,Slot2,NH New History-2,03,,07,,Comedy,16,,06,,")
 replacedDateVal = IncrementAndReplaceYear(oriDate)
 MsgBox replacedDateVal
End Sub

 

'' Regular expression to get the dateVal from String

Function GetDateFromString(strToSearch)
 Set re = New RegExp
 re.Pattern = "(\d{1,2}\/\d{1,2}\/\d{4})|"
 Set resStr = re.Execute(strToSearch)
 If re.Test(strToSearch) Then
  GetDateFromString = resStr(0).Value
 Else
  GetDateFromString = "date not found!"
 End If
End Function

 

'' Increment the year value and replace the year val
Function IncrementAndReplaceYear(dateVal)
 yr = Year(dateVal)
 replacedVal = ConvertDate(dateVal, yr + 1)
 IncrementAndReplaceYear = replacedVal
End Function

 

Function ConvertDate(originalDate, incrementedYear)
 dateVal = Day(originalDate)
 monthVal = Month(originalDate)
 If Len(dateVal) = 1 Then
   dateVal = "0" & dateVal
 End If
If Len(monthVal) = 1 Then
 monthVal = "0" & monthVal
End If
 ConvertDate = dateVal & "/" & monthVal & "/" & incrementedYear
End Function

New Here?
Join us and watch the welcome video:
Watch the new Interview