Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 281
  • Last Modified:

How do i search using Regex?

Experts
I have a long string comming in from database where i am telling the user that your registration expires on a particular date and so on.
What i want to do is that i want to search for this date in that text and i want to replace it with another date that also comes from my database.
I want to use regex here to check whether there is first of all anything like a date in text and second it is a valid date and then third if the first 2 are true replacing with the expiration date from another object.
I would appreciate your opinion about this.
Thanks
0
parveenmehta
Asked:
parveenmehta
  • 2
  • 2
1 Solution
 
ozoCommented:
Are the dates in the file in any particular format/language?
Are there some sets of dates that are not consider valid for your purposes?
If so, what should be done with them?
Are there things other than dates in the file?
0
 
parveenmehtaAuthor Commented:
Yes the date formate is mm/dd/yyyy.As long as there is a valid date i should be able to take that and replace  that with the available expiration date.Yes there is a text "Your certification expires on 05/01/2006".So i want to be able to replace the expiration date at the end.
0
 
parveenmehtaAuthor Commented:
This is what i have done so far.But the default date that is comming in the textbox is not getting replaced.
string dt=dsexpdt.Tables[0].Rows[0]["ExpirationDate"].ToString();
txtcell5.InnerHtml=Regex.Replace(txtcell5.InnerHtml,@"(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d",dt);
Can anyone please suggest ?
Thanks                  
0
 
ozoCommented:
Might it be easier to search for "Your certification expires on" ?
0
 
HonorGodCommented:
Challenges:
- Given the "text string" (i.e., the message) in some string variable (e.g., msg):
   msg = "Your certification expires on 05/01/2006";

- You can use the following regexp to determine that a field exists within the string that "appears to be" a date:
----------------------------------------------------------------------
<html>
<head>
<title>Regular Expression - date</title>
<script type='text/javascript'>
  function checkIt( obj ) {
    var re = /(\d{1,2})([-\/])(\d{1,2})\2((19|20)(\d{4}|\d{2}))$/;
    var result = re.exec( obj.value );
    var info   = '';
    obj.value = 'Month: ' + result[ 1 ] + '  Day: ' + result[ 3 ] + '  Year: ' + result[ 4 ];  
  }
</script>
</head>
<body>
  <input type='text' size='50' onfocus='checkIt(this);' value='Your certification expires on 05/01/2006'>Testing</input>
</body>
</html>
----------------------------------------------------------------------
- You should note, however, that the month and day fields are positionally dependent.
  This does not take locale into account.  So, if the user enters:  DD/MM/YYYY
  The the code will mistakenly put the value of DD right after the "Month: "...
0

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now