Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Regular expressions in C#

Regular expressions in C#

Scheduled Pinned Locked Moved C#
regexcsharphelptutorial
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    KeithF
    wrote on last edited by
    #1

    Hi All, I need to compare a string to a regular expression to determine if the string is in this format. I am setting the string equal to a line read from a file. Once i have read the line i want to compare it to a regx to determine if the string is formatted as follows: Date Time (5-10 spaces) 123-12-123456 An example line: 09-02-2009 10:33 001-01-218913 The code i have currently is:

    strFile = "C:\\test.txt";
    using (StreamReader srTlog = new StreamReader(new FileStream(strFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)))
    {
    while ((strLine = srTlog.ReadLine()) != null)
    {
    Regex pattern=new Regex("NOT SURE WHAT GOES HERE");
    if(pattern.IsMatch(strToCheck))
    {
    //DO WHATEVER NOW
    }
    }
    }

    I would appreciate any help. Thanks

    W P R 3 Replies Last reply
    0
    • K KeithF

      Hi All, I need to compare a string to a regular expression to determine if the string is in this format. I am setting the string equal to a line read from a file. Once i have read the line i want to compare it to a regx to determine if the string is formatted as follows: Date Time (5-10 spaces) 123-12-123456 An example line: 09-02-2009 10:33 001-01-218913 The code i have currently is:

      strFile = "C:\\test.txt";
      using (StreamReader srTlog = new StreamReader(new FileStream(strFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)))
      {
      while ((strLine = srTlog.ReadLine()) != null)
      {
      Regex pattern=new Regex("NOT SURE WHAT GOES HERE");
      if(pattern.IsMatch(strToCheck))
      {
      //DO WHATEVER NOW
      }
      }
      }

      I would appreciate any help. Thanks

      W Offline
      W Offline
      whaka
      wrote on last edited by
      #2

      string patternStr = @"^\d{2}-\d{2}-\d{4} \d{2}:\d{2} \d{3}-\d{2}-\d{6}$"; ^ is beginning of line $ is end of line \d is any digit It should match the given string, although it will also match strings with invalid dates such as 99-99-2009 34:10 123-45-123456 But if you know that the datestring will always be correct and the important thing is the last bit, the pattern above should work fine (didnt test it though).

      1 Reply Last reply
      0
      • K KeithF

        Hi All, I need to compare a string to a regular expression to determine if the string is in this format. I am setting the string equal to a line read from a file. Once i have read the line i want to compare it to a regx to determine if the string is formatted as follows: Date Time (5-10 spaces) 123-12-123456 An example line: 09-02-2009 10:33 001-01-218913 The code i have currently is:

        strFile = "C:\\test.txt";
        using (StreamReader srTlog = new StreamReader(new FileStream(strFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)))
        {
        while ((strLine = srTlog.ReadLine()) != null)
        {
        Regex pattern=new Regex("NOT SURE WHAT GOES HERE");
        if(pattern.IsMatch(strToCheck))
        {
        //DO WHATEVER NOW
        }
        }
        }

        I would appreciate any help. Thanks

        P Offline
        P Offline
        psi_u
        wrote on last edited by
        #3

        // Find formats: dd/mm/yy or dd/mm/yyy, seperators: -/. (i.e. 31.01.08 31-01-08 etc.)
        Regex rx = new Regex(@"([1-9]|0[1-9]|[12][0-9]|3[01])[- /.]([1-9]|0[1-9]|1[012])[- /.]((19|20)\d\d|\d\d)");

        // Define a test string.
        string text = fileName;

        // Find matches.
        MatchCollection matches = rx.Matches(text);

        // Report the number of matches found.
        if (matches.Count > 1)
        DoST();

        // Report on each match.
        foreach (Match match in matches)
        {
        textBoxDocumentDate.Text = match.Value;
        }

        1 Reply Last reply
        0
        • K KeithF

          Hi All, I need to compare a string to a regular expression to determine if the string is in this format. I am setting the string equal to a line read from a file. Once i have read the line i want to compare it to a regx to determine if the string is formatted as follows: Date Time (5-10 spaces) 123-12-123456 An example line: 09-02-2009 10:33 001-01-218913 The code i have currently is:

          strFile = "C:\\test.txt";
          using (StreamReader srTlog = new StreamReader(new FileStream(strFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)))
          {
          while ((strLine = srTlog.ReadLine()) != null)
          {
          Regex pattern=new Regex("NOT SURE WHAT GOES HERE");
          if(pattern.IsMatch(strToCheck))
          {
          //DO WHATEVER NOW
          }
          }
          }

          I would appreciate any help. Thanks

          R Offline
          R Offline
          realJSOP
          wrote on last edited by
          #4

          Why go through the hassle of using regex? Just use DateTime.ParseExact, and handle the bool return value.

          "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
          -----
          "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups