How do i search using Regex?

Posted on 2006-05-15
Last Modified: 2010-04-17
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.
Question by:parveenmehta
    LVL 84

    Expert Comment

    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?

    Author Comment

    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.

    Author Comment

    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 ?
    LVL 84

    Expert Comment

    Might it be easier to search for "Your certification expires on" ?
    LVL 41

    Accepted Solution

    - 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:
    <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 ];  
      <input type='text' size='50' onfocus='checkIt(this);' value='Your certification expires on 05/01/2006'>Testing</input>
    - 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: "...

    Featured Post

    Course: HTML5 Mobile App Development with PhoneGap

    PhoneGap can help you leverage your already existing HTML5, JavaScript, and CSS skills in order to create and deploy cross-platform mobile apps.This program comprehensively covers HTML5 mobile app development from top to bottom.

    Join & Write a Comment

    Suggested Solutions

    Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
    Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
    In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
    In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now