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. Regex

Regex

Scheduled Pinned Locked Moved C#
questionregexhelptutorial
6 Posts 3 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.
  • D Offline
    D Offline
    Dromar
    wrote on last edited by
    #1

    Hi everyone, I use Regex for splitting a string. Now i have 2 Questions: 1. I have e.g the string "something || anything && anotherthing" and I want to split it into string[3] ={ something, anything, anotherthing }; is there an easier way than to use 2 Regex like Regex("||") and Regex("&&") and split it first with one Regex and then each element with the other? 2. I have heard that when you split a string you can leave the splitsequence (like in the example before "||") to one of the stringsresults. How can I do that? Thanks for your help!

    M S 2 Replies Last reply
    0
    • D Dromar

      Hi everyone, I use Regex for splitting a string. Now i have 2 Questions: 1. I have e.g the string "something || anything && anotherthing" and I want to split it into string[3] ={ something, anything, anotherthing }; is there an easier way than to use 2 Regex like Regex("||") and Regex("&&") and split it first with one Regex and then each element with the other? 2. I have heard that when you split a string you can leave the splitsequence (like in the example before "||") to one of the stringsresults. How can I do that? Thanks for your help!

      M Offline
      M Offline
      Maxim Langman
      wrote on last edited by
      #2

      If you have the same delimiter between strings, then the easiest way is to use Split() method of string class.

      string str = "aaa::bbb::ccc";
      string[] splittedStrings = str.Split("::");

      But, if the explression is more difficult, then Regex is a good solution

      1 Reply Last reply
      0
      • D Dromar

        Hi everyone, I use Regex for splitting a string. Now i have 2 Questions: 1. I have e.g the string "something || anything && anotherthing" and I want to split it into string[3] ={ something, anything, anotherthing }; is there an easier way than to use 2 Regex like Regex("||") and Regex("&&") and split it first with one Regex and then each element with the other? 2. I have heard that when you split a string you can leave the splitsequence (like in the example before "||") to one of the stringsresults. How can I do that? Thanks for your help!

        S Offline
        S Offline
        snorkie
        wrote on last edited by
        #3

        Why not just use the following regex: "( \|\| )|( && )" It finds either and can be used in your split. Hogan

        D 1 Reply Last reply
        0
        • S snorkie

          Why not just use the following regex: "( \|\| )|( && )" It finds either and can be used in your split. Hogan

          D Offline
          D Offline
          Dromar
          wrote on last edited by
          #4

          Somehow that is not working... I tried Regex reg = new Regex("(\\|\\|)(&&)"); on string input = "something || anything && anotherthing"; but it isnt Spliting it even once :(

          S 1 Reply Last reply
          0
          • D Dromar

            Somehow that is not working... I tried Regex reg = new Regex("(\\|\\|)(&&)"); on string input = "something || anything && anotherthing"; but it isnt Spliting it even once :(

            S Offline
            S Offline
            snorkie
            wrote on last edited by
            #5

            Try again with the correct string :) Regex reg = new Regex("( \|\| )|( && )"); Hogan

            D 1 Reply Last reply
            0
            • S snorkie

              Try again with the correct string :) Regex reg = new Regex("( \|\| )|( && )"); Hogan

              D Offline
              D Offline
              Dromar
              wrote on last edited by
              #6

              Oh there was an | between the brackets... Thanks a lot, and this also solves Question 2 :)

              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