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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. How to extract a list of substrings from a string which match specified format using Regex

How to extract a list of substrings from a string which match specified format using Regex

Scheduled Pinned Locked Moved C#
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.
  • 1 Offline
    1 Offline
    12Code
    wrote on last edited by
    #1

    For exemple, i have a string = "**%alan#**23**%12:34:50#**ab **%regular#**]r"; Extract string which enclosed by '%' and '#' The substring result should be substr[0] = "alan"; substr[1] = "12:34:50"; substr[2] = "regular"; How to do it in Regex?

    8.Kelvin()
    {
    while (!(the machine can program itself))
    Wont_stop_coding = true;
    }

    C 1 Reply Last reply
    0
    • 1 12Code

      For exemple, i have a string = "**%alan#**23**%12:34:50#**ab **%regular#**]r"; Extract string which enclosed by '%' and '#' The substring result should be substr[0] = "alan"; substr[1] = "12:34:50"; substr[2] = "regular"; How to do it in Regex?

      8.Kelvin()
      {
      while (!(the machine can program itself))
      Wont_stop_coding = true;
      }

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      Read up on grouping in regex. You can do exactly this by specifying which parts of your match to return.

      Christian Graus Driven to the arms of OSX by Vista. "I am new to programming world. I have been learning c# for about past four weeks. I am quite acquainted with the fundamentals of c#. Now I have to work on a project which converts given flat files to XML using the XML serialization method" - SK64 ( but the forums have stuff like this posted every day )

      1 1 Reply Last reply
      0
      • C Christian Graus

        Read up on grouping in regex. You can do exactly this by specifying which parts of your match to return.

        Christian Graus Driven to the arms of OSX by Vista. "I am new to programming world. I have been learning c# for about past four weeks. I am quite acquainted with the fundamentals of c#. Now I have to work on a project which converts given flat files to XML using the XML serialization method" - SK64 ( but the forums have stuff like this posted every day )

        1 Offline
        1 Offline
        12Code
        wrote on last edited by
        #3

        i tried this code..

                string str = "%alan#23%12:34:50#ab %regular#\]r";
        
                Regex RE = new Regex(@"(\\%+)\\#"); 
                MatchCollection MC = RE.Matches(str);
        
                if (MC.Count == 0) MessageBox.Show("No match");
                foreach (Match M in MC)
                {
                    MessageBox.Show(M.ToString());
                }
        

        But when execute it, i only got "No match" message box. :(

        Christian Graus wrote:

        grouping in regex

        I'm not good in grouping regex, pls advise..

        8.Kelvin()
        {
        while (!(the machine can program itself))
        Wont_stop_coding = true;
        }

        1 1 Reply Last reply
        0
        • 1 12Code

          i tried this code..

                  string str = "%alan#23%12:34:50#ab %regular#\]r";
          
                  Regex RE = new Regex(@"(\\%+)\\#"); 
                  MatchCollection MC = RE.Matches(str);
          
                  if (MC.Count == 0) MessageBox.Show("No match");
                  foreach (Match M in MC)
                  {
                      MessageBox.Show(M.ToString());
                  }
          

          But when execute it, i only got "No match" message box. :(

          Christian Graus wrote:

          grouping in regex

          I'm not good in grouping regex, pls advise..

          8.Kelvin()
          {
          while (!(the machine can program itself))
          Wont_stop_coding = true;
          }

          1 Offline
          1 Offline
          12Code
          wrote on last edited by
          #4

          I think i got it.. the regex should be: Regex RE = new Regex(@"(\%(\w|\: )+)\#");

          8.Kelvin()
          {
          while (!(the machine can program itself))
          Wont_stop_coding = true;
          }

          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