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. Double characters in regexp

Double characters in regexp

Scheduled Pinned Locked Moved C#
regextutorialquestion
4 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.
  • M Offline
    M Offline
    Markus Pylvnen
    wrote on last edited by
    #1

    I am trying to use following regular expression to parse strings:

    //\s*{\s*__(?<name>[^ ]+)__\s*(?<parameters>#[^}]*)}

    Strings I try to parse are for example:

    // {__NAME1__}
    // {__NAME2__#param1=2}
    // {__NAME_3__ #param1=3, #param4=5 }

    My regexp should match all strings above. Name can contain one underline character (_) but not two sequential characters (__). Parameters start with first #-character and end to }-character. If there isn't any parameters my regexp goes mad and takes next next line to <name>. How I have to change my regexp so <name> doesn't allow double _-characters ?

    T S 2 Replies Last reply
    0
    • M Markus Pylvnen

      I am trying to use following regular expression to parse strings:

      //\s*{\s*__(?<name>[^ ]+)__\s*(?<parameters>#[^}]*)}

      Strings I try to parse are for example:

      // {__NAME1__}
      // {__NAME2__#param1=2}
      // {__NAME_3__ #param1=3, #param4=5 }

      My regexp should match all strings above. Name can contain one underline character (_) but not two sequential characters (__). Parameters start with first #-character and end to }-character. If there isn't any parameters my regexp goes mad and takes next next line to <name>. How I have to change my regexp so <name> doesn't allow double _-characters ?

      T Offline
      T Offline
      Tormod Fjeldskaar
      wrote on last edited by
      #2

      This should do it: //\s*{\s*__(?.*(?=__))__\s*(?\x23[^}]*)*}

      M 1 Reply Last reply
      0
      • M Markus Pylvnen

        I am trying to use following regular expression to parse strings:

        //\s*{\s*__(?<name>[^ ]+)__\s*(?<parameters>#[^}]*)}

        Strings I try to parse are for example:

        // {__NAME1__}
        // {__NAME2__#param1=2}
        // {__NAME_3__ #param1=3, #param4=5 }

        My regexp should match all strings above. Name can contain one underline character (_) but not two sequential characters (__). Parameters start with first #-character and end to }-character. If there isn't any parameters my regexp goes mad and takes next next line to <name>. How I have to change my regexp so <name> doesn't allow double _-characters ?

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

        Try the following. I put your two expressions into my Regex editor (http://www.radsoftware.com.au/regexdesigner/) and started playing around till I found the following to work. \s*{\s*__(?[^ ]+)__(\s*(?#[^}]*)})? It works because the second part of the expression (\s*(?#[^}]*)})? is now in an un-named optional group. Your first example, // {__NAME1__} didn't match the second part of your expression, so it failed. The other two examples matched, so they came back. This will result in an extra group being returned, but you can ignore it when you go through your matches by using the group names. Hope this helps. Hogan

        1 Reply Last reply
        0
        • T Tormod Fjeldskaar

          This should do it: //\s*{\s*__(?.*(?=__))__\s*(?\x23[^}]*)*}

          M Offline
          M Offline
          Markus Pylvnen
          wrote on last edited by
          #4

          Problem solved, thanks!

          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