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 expression help

RegEx expression help

Scheduled Pinned Locked Moved C#
regexquestionhelptutorial
31 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.
  • F Fayu

    Works like a charm!!! Any way I can just have the value returned?

    N Offline
    N Offline
    NavnathKale
    wrote on last edited by
    #20

    Now time to callback my initial post. :| Like I said use Named capturing groups. Using this you can just return the value of provided propertyname

    Regex regex = new Regex(@"(\[{3}MyProperty3\]{3}[\s{1}])(?<MyProperty3>.*?(?=\s{1}\[|$))");
    Match regexmatch = regex.Match(@"[[[MyProperty1]]] Value [[[MyProperty2]]] value2 [[[MyProperty3]]] value 3");
    string value = regexmatch.Groups["MyProperty3"].Value

    Above code will return value of property MyProperty3

    1 Reply Last reply
    0
    • F Fayu

      I would LOVE an explanation on the logic behind this... (\[{3}Property2\]{3}[\s{1}])(.*?(?=\s{1}\[|$)) The only part I understand is (\[{3}Property2\]{3}[\s{1}])

      N Offline
      N Offline
      NavnathKale
      wrote on last edited by
      #21

      Yeah!!! u ought to know everything thats going around on earth :wtf: Google will take you to right place if you search for positive lookahead in regex :-\

      1 Reply Last reply
      0
      • P PIEBALDconsult

        I prefer the technique I showed. You then enumerate the Matches and fill a Dictionary with the Names and Values. Your requirements 1 -- 3 are then satisfied by the Dictionary and you only have to execute the RegEx once per group of properties.

        Fayu wrote:

        Use VBScript

        Then why post it in the C# forum? Or maybe you should give more information on the bigger picture of what you are trying to accomplish.

        N Offline
        N Offline
        NavnathKale
        wrote on last edited by
        #22

        Your solution will also give same result, but it will return all properties. As Fayu asked on his requirement, it wasnt he is looking for. Cheers !!!

        P 1 Reply Last reply
        0
        • N NavnathKale

          Your solution will also give same result, but it will return all properties. As Fayu asked on his requirement, it wasnt he is looking for. Cheers !!!

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

          Navnath R. Kale wrote:

          it wasnt he is looking for

          It should be.

          1 Reply Last reply
          0
          • P PIEBALDconsult

            I prefer the technique I showed. You then enumerate the Matches and fill a Dictionary with the Names and Values. Your requirements 1 -- 3 are then satisfied by the Dictionary and you only have to execute the RegEx once per group of properties.

            Fayu wrote:

            Use VBScript

            Then why post it in the C# forum? Or maybe you should give more information on the bigger picture of what you are trying to accomplish.

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

            Fayu wrote:

            I wish I can use the CLR but the requirements I have prevent me from using the CLR.

            ::Throws up hands::

            F 1 Reply Last reply
            0
            • P PIEBALDconsult

              Fayu wrote:

              I wish I can use the CLR but the requirements I have prevent me from using the CLR.

              ::Throws up hands::

              F Offline
              F Offline
              Fayu
              wrote on last edited by
              #25

              Yea but I am not using the clr. The pattern he provided worked with all requirement, including not using CLR. I used his pattern using VBScript.RegexExp and it worked wonders. Thanks for your help as well.

              P 1 Reply Last reply
              0
              • F Fayu

                Yea but I am not using the clr. The pattern he provided worked with all requirement, including not using CLR. I used his pattern using VBScript.RegexExp and it worked wonders. Thanks for your help as well.

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

                Mine works with perl: perl -e "print \"[[[MyProperty1]]] Value [[[MyProperty2]]] value2 [[[MyProperty3]]] value 3\" =~ m/(?:\[\[\[(?'Name'.*?)\]\]\](?'Value'[^\[]*))/g"

                %stuff = $ARGV[0] =~ m/(?:\[\[\[(?'Name'.*?)\]\]\](?'Value'[^\[]*))/g ;

                foreach $id ( sort ( keys ( %stuff ) ) )
                {
                print ( "$id = $stuff{$id}\n" ) ;
                }

                modified on Saturday, April 10, 2010 12:33 AM

                N 1 Reply Last reply
                0
                • P PIEBALDconsult

                  Mine works with perl: perl -e "print \"[[[MyProperty1]]] Value [[[MyProperty2]]] value2 [[[MyProperty3]]] value 3\" =~ m/(?:\[\[\[(?'Name'.*?)\]\]\](?'Value'[^\[]*))/g"

                  %stuff = $ARGV[0] =~ m/(?:\[\[\[(?'Name'.*?)\]\]\](?'Value'[^\[]*))/g ;

                  foreach $id ( sort ( keys ( %stuff ) ) )
                  {
                  print ( "$id = $stuff{$id}\n" ) ;
                  }

                  modified on Saturday, April 10, 2010 12:33 AM

                  N Offline
                  N Offline
                  NavnathKale
                  wrote on last edited by
                  #27

                  Well i guess, all this confusion is because question is in wrong section. Perl supports Named capture groups but client scripts don't.

                  P 1 Reply Last reply
                  0
                  • N NavnathKale

                    Well i guess, all this confusion is because question is in wrong section. Perl supports Named capture groups but client scripts don't.

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

                    Navnath R. Kale wrote:

                    Named capture groups

                    You don't need to name them. The important thing is to have the engine return all the matches and enumerate them.

                    N 1 Reply Last reply
                    0
                    • P PIEBALDconsult

                      Navnath R. Kale wrote:

                      Named capture groups

                      You don't need to name them. The important thing is to have the engine return all the matches and enumerate them.

                      N Offline
                      N Offline
                      NavnathKale
                      wrote on last edited by
                      #29

                      :doh: !!! Now the thread becomes the session. Actually (?'Name') and (?'Value') is equal to (?<Name>) and (?<Value>) and is nothing but Named capturing group. .Net supports it both way.

                      P 1 Reply Last reply
                      0
                      • N NavnathKale

                        :doh: !!! Now the thread becomes the session. Actually (?'Name') and (?'Value') is equal to (?<Name>) and (?<Value>) and is nothing but Named capturing group. .Net supports it both way.

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

                        Yes. I prefer the apostrophes -- at least I don't need to escape them when I post them. :-D

                        N 1 Reply Last reply
                        0
                        • P PIEBALDconsult

                          Yes. I prefer the apostrophes -- at least I don't need to escape them when I post them. :-D

                          N Offline
                          N Offline
                          NavnathKale
                          wrote on last edited by
                          #31

                          Yeah :laugh: On first post I was like " :doh: WTF happened to my pattern, it was correct when I typed"

                          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