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. Regex.Split with a pipe | as separator

Regex.Split with a pipe | as separator

Scheduled Pinned Locked Moved C#
regexhelpquestion
3 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.
  • G Offline
    G Offline
    GDavy
    wrote on last edited by
    #1

    I wanted to use the Regex.Split function to split an incoming string up. The problem is that the string parts are separated by a pipe | but the pipe is used inside the Split function to split different separators (e.g. if you want to split on dash and space you would write it as " |-"). So when I use "|" the string splits into it's characters. I tried using "\|" but this is considered an Unrecognized Escape Character and doesn't compile. Does anyone know if this is possible in another way?

    G 1 Reply Last reply
    0
    • G GDavy

      I wanted to use the Regex.Split function to split an incoming string up. The problem is that the string parts are separated by a pipe | but the pipe is used inside the Split function to split different separators (e.g. if you want to split on dash and space you would write it as " |-"). So when I use "|" the string splits into it's characters. I tried using "\|" but this is considered an Unrecognized Escape Character and doesn't compile. Does anyone know if this is possible in another way?

      G Offline
      G Offline
      Guffa
      wrote on last edited by
      #2

      You have to encode the characters in the expression twice. First you have to encode the pipe to put it in the regular expression, then you have to encode the regular expression to put it in a literal string in the code. Use \\| in a regular literal string, or \| in an @ delimited string. Example: string pattern = "\\|"; or string pattern = @"\|";

      Despite everything, the person most likely to be fooling you next is yourself.

      G 1 Reply Last reply
      0
      • G Guffa

        You have to encode the characters in the expression twice. First you have to encode the pipe to put it in the regular expression, then you have to encode the regular expression to put it in a literal string in the code. Use \\| in a regular literal string, or \| in an @ delimited string. Example: string pattern = "\\|"; or string pattern = @"\|";

        Despite everything, the person most likely to be fooling you next is yourself.

        G Offline
        G Offline
        GDavy
        wrote on last edited by
        #3

        Thanks, that solved the problem:)

        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