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 [modified]

Regular Expressions [modified]

Scheduled Pinned Locked Moved C#
regexhelptutorialquestionlearning
4 Posts 2 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.
  • M Offline
    M Offline
    Mikey_H
    wrote on last edited by
    #1

    I am learning how to read text files at the moment, and it seems regular expressions is the way to go for what I need. I understand the concept but not quite sure how to implement it. I have the line... $2 USD NL Texas Hold'em - Sunday, August 09, 19:05:42 EDT 2009 ...stored in a string. I would like to take certain parts of this and store them in seperate strings. The data I need is... NL Texas Hold'em August 09 19:05:42 2009 Should I use something like "USD\s.*\s-" this would give me "USD NL Texas Hold'Em -"? After this I can then trim "USD " and " -" off the start and the end of the string and I will the information I need.

    Regex rExp = new Regex("USD\\s.*\\s-");
    Match match = rExp.Match(sLine1);

    string gameType = match.Value;
    gameType.TrimStart("USD ".ToCharArray());
    gameType.TrimEnd(" -".ToCharArray());

    switch (gameType)
    {
    case "NL Texas Hold'em":
    // Do something
    break;
    default:
    break;
    }

    Is this a good way to approach the task? Are there better ways to do this? Any help would be appreciated. Thank you.

    modified on Monday, August 10, 2009 1:18 AM

    P 1 Reply Last reply
    0
    • M Mikey_H

      I am learning how to read text files at the moment, and it seems regular expressions is the way to go for what I need. I understand the concept but not quite sure how to implement it. I have the line... $2 USD NL Texas Hold'em - Sunday, August 09, 19:05:42 EDT 2009 ...stored in a string. I would like to take certain parts of this and store them in seperate strings. The data I need is... NL Texas Hold'em August 09 19:05:42 2009 Should I use something like "USD\s.*\s-" this would give me "USD NL Texas Hold'Em -"? After this I can then trim "USD " and " -" off the start and the end of the string and I will the information I need.

      Regex rExp = new Regex("USD\\s.*\\s-");
      Match match = rExp.Match(sLine1);

      string gameType = match.Value;
      gameType.TrimStart("USD ".ToCharArray());
      gameType.TrimEnd(" -".ToCharArray());

      switch (gameType)
      {
      case "NL Texas Hold'em":
      // Do something
      break;
      default:
      break;
      }

      Is this a good way to approach the task? Are there better ways to do this? Any help would be appreciated. Thank you.

      modified on Monday, August 10, 2009 1:18 AM

      P Offline
      P Offline
      PIEBALDconsult
      wrote on last edited by
      #2

      Ah, thanks, I was just working on a Regex tester; you gave me something to test! :-D With that input, this Regex: \$\d* \w* (?'Game'.*?) -.*?, (?'Date'.*?), (?'Time'\d\d:\d\d:\d\d) \w* (?'Year'\d*) Produces:

      Match: $2 USD NL Texas Hold'em - Sunday, August 09, 19:05:42 EDT 2009
      Group 0: $2 USD NL Texas Hold'em - Sunday, August 09, 19:05:42 EDT 2009
      Group Game: NL Texas Hold'em
      Group Date: August 09
      Group Time: 19:05:42
      Group Year: 2009

      (You should also look into Expresso.)

      M 1 Reply Last reply
      0
      • P PIEBALDconsult

        Ah, thanks, I was just working on a Regex tester; you gave me something to test! :-D With that input, this Regex: \$\d* \w* (?'Game'.*?) -.*?, (?'Date'.*?), (?'Time'\d\d:\d\d:\d\d) \w* (?'Year'\d*) Produces:

        Match: $2 USD NL Texas Hold'em - Sunday, August 09, 19:05:42 EDT 2009
        Group 0: $2 USD NL Texas Hold'em - Sunday, August 09, 19:05:42 EDT 2009
        Group Game: NL Texas Hold'em
        Group Date: August 09
        Group Time: 19:05:42
        Group Year: 2009

        (You should also look into Expresso.)

        M Offline
        M Offline
        Mikey_H
        wrote on last edited by
        #3

        Thanks Piebald, thats the sort of expression I was looking for. Far more simpl than what I had originally.

        // Parse line 2 to find game type, date, and time of hand
        Regex regExp = new Regex("$\\d* USD (?'GameType'.*?) -.*?, (?'DateMonth'\\w*?) (?'DateDay'\\d*?), (?'DateTime'\\d{2}:\\d{2}:\\d{2}?) \\w* (?'DateYear' \\d*?)");
        Match line2Match = regExp.Match(sLine2);

        switch (line2Match.Groups["GameType"].Value)
        {
        case "NL Texas Hold'em":
        // Do something
        break;
        default:
        break;
        }

        P 1 Reply Last reply
        0
        • M Mikey_H

          Thanks Piebald, thats the sort of expression I was looking for. Far more simpl than what I had originally.

          // Parse line 2 to find game type, date, and time of hand
          Regex regExp = new Regex("$\\d* USD (?'GameType'.*?) -.*?, (?'DateMonth'\\w*?) (?'DateDay'\\d*?), (?'DateTime'\\d{2}:\\d{2}:\\d{2}?) \\w* (?'DateYear' \\d*?)");
          Match line2Match = regExp.Match(sLine2);

          switch (line2Match.Groups["GameType"].Value)
          {
          case "NL Texas Hold'em":
          // Do something
          break;
          default:
          break;
          }

          P Offline
          P Offline
          PIEBALDconsult
          wrote on last edited by
          #4

          Glad to be of service.

          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