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. Split string with Regex

Split string with Regex

Scheduled Pinned Locked Moved C#
regexquestion
7 Posts 7 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.
  • R Offline
    R Offline
    Ronald de Boer
    wrote on last edited by
    #1

    Hi, I need to split a large string and split that splitted string again. I have the following string:

    root
    {
    stringB =
    {
    "key": "value",
    "key": "value",
    "StringC":
    {
    "key": "value",
    "key": "value",
    "key": "value",
    "key": "value",
    }
    "StringD": "value",
    "StringE":
    {
    "key": "value",
    }
    }
    }

    The result I actually want is something like this: string[] AllStrings = StringB,StringC,StringD,StringE Is there a way to do this? I used Regex.Split option. Thanks in advance!

    P L R A realJSOPR 6 Replies Last reply
    0
    • R Ronald de Boer

      Hi, I need to split a large string and split that splitted string again. I have the following string:

      root
      {
      stringB =
      {
      "key": "value",
      "key": "value",
      "StringC":
      {
      "key": "value",
      "key": "value",
      "key": "value",
      "key": "value",
      }
      "StringD": "value",
      "StringE":
      {
      "key": "value",
      }
      }
      }

      The result I actually want is something like this: string[] AllStrings = StringB,StringC,StringD,StringE Is there a way to do this? I used Regex.Split option. Thanks in advance!

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

      That doesn't look regular enough. And try the Regex forum.

      1 Reply Last reply
      0
      • R Ronald de Boer

        Hi, I need to split a large string and split that splitted string again. I have the following string:

        root
        {
        stringB =
        {
        "key": "value",
        "key": "value",
        "StringC":
        {
        "key": "value",
        "key": "value",
        "key": "value",
        "key": "value",
        }
        "StringD": "value",
        "StringE":
        {
        "key": "value",
        }
        }
        }

        The result I actually want is something like this: string[] AllStrings = StringB,StringC,StringD,StringE Is there a way to do this? I used Regex.Split option. Thanks in advance!

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        You could probably do it with String.Split()[^].

        The best things in life are not things.

        1 Reply Last reply
        0
        • R Ronald de Boer

          Hi, I need to split a large string and split that splitted string again. I have the following string:

          root
          {
          stringB =
          {
          "key": "value",
          "key": "value",
          "StringC":
          {
          "key": "value",
          "key": "value",
          "key": "value",
          "key": "value",
          }
          "StringD": "value",
          "StringE":
          {
          "key": "value",
          }
          }
          }

          The result I actually want is something like this: string[] AllStrings = StringB,StringC,StringD,StringE Is there a way to do this? I used Regex.Split option. Thanks in advance!

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

          I believe you are trying to parse a JSON string. If yes then this link might be of some help to you!

          1 Reply Last reply
          0
          • R Ronald de Boer

            Hi, I need to split a large string and split that splitted string again. I have the following string:

            root
            {
            stringB =
            {
            "key": "value",
            "key": "value",
            "StringC":
            {
            "key": "value",
            "key": "value",
            "key": "value",
            "key": "value",
            }
            "StringD": "value",
            "StringE":
            {
            "key": "value",
            }
            }
            }

            The result I actually want is something like this: string[] AllStrings = StringB,StringC,StringD,StringE Is there a way to do this? I used Regex.Split option. Thanks in advance!

            A Offline
            A Offline
            anishkannan
            wrote on last edited by
            #5

            You can parse using JavaScriptSerializer.. This may help to you

            string s = "{\"StringC\":"+
            "{"+
            "\"key1\": \"value\"," +
            "\"key2\": \"value\"," +
            "\"key3\": \"value\"," +
            "\"key4\": \"value\"" +
            "},"+
            "\"StringD\": \"value\"," +
            "\"StringE\":" +
            "{"+
            "\"key1\": \"value\"" +
            "}"+
            "}";

                    JavaScriptSerializer f =new JavaScriptSerializer();
            
                    JsonData jsd = f.Deserialize<JsonData>(s);
            

            public class JsonData
            {
            public Hashtable StringC { get; set; }
            public string StringD {get; set;}
            public Hashtable StringE { get; set; }
            }

            1 Reply Last reply
            0
            • R Ronald de Boer

              Hi, I need to split a large string and split that splitted string again. I have the following string:

              root
              {
              stringB =
              {
              "key": "value",
              "key": "value",
              "StringC":
              {
              "key": "value",
              "key": "value",
              "key": "value",
              "key": "value",
              }
              "StringD": "value",
              "StringE":
              {
              "key": "value",
              }
              }
              }

              The result I actually want is something like this: string[] AllStrings = StringB,StringC,StringD,StringE Is there a way to do this? I used Regex.Split option. Thanks in advance!

              realJSOPR Offline
              realJSOPR Offline
              realJSOP
              wrote on last edited by
              #6

              Just so you know, your sample string isn't very consistent. I would think there would have to be a comma after the closing braces. Beyond that, it seems to me you want to Split on the "{" character, and then the "," character on each of the first split result strings. I would probably write a method that accepted the delimiter character and the string to be split that returned an array of split items. Regex is not what you want to use - sometimes, you just gotta write the code.

              ".45 ACP - because shooting twice is just silly" - JSOP, 2010
              -----
              You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
              -----
              "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

              1 Reply Last reply
              0
              • R Ronald de Boer

                Hi, I need to split a large string and split that splitted string again. I have the following string:

                root
                {
                stringB =
                {
                "key": "value",
                "key": "value",
                "StringC":
                {
                "key": "value",
                "key": "value",
                "key": "value",
                "key": "value",
                }
                "StringD": "value",
                "StringE":
                {
                "key": "value",
                }
                }
                }

                The result I actually want is something like this: string[] AllStrings = StringB,StringC,StringD,StringE Is there a way to do this? I used Regex.Split option. Thanks in advance!

                L Offline
                L Offline
                Lutoslaw
                wrote on last edited by
                #7

                Try

                (.*((\"([\d\w]+)\":)|(\s([\d\w]+) =)))*

                The group no 4 contains keys like key or stringC and group 6 contins keys like StringB EDIT: Of course you need to code

                string expr = @"(.*((\"([\d\w]+)\":)|(\s([\d\w]+) =)))*";
                var m = Regex.Match(expr);
                var group4 = m.Groups[4];
                var group6 = m.Group[6];
                string[] allStrings = group4.Captures.Select(c=>c.Value).Union(group6.Select(c=>c.Value)).ToArray();

                captures now contains strings you need DISCLAIMER: 1. not tested. 2. use named groups if you want to.

                Greetings - Jacek

                modified on Wednesday, June 15, 2011 3:38 AM

                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