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 Offline
    F Offline
    Fayu
    wrote on last edited by
    #1

    I am new to this whole regex thing. I have read up on it but i still need some help. I have expressions (shown below) and I want to get the values (substring). For example, in example 1 i want to search for my property ([[[MyProperty1]]]) and I want to get back 'Value'. In example 2, i want to be able to search for [[[MyProperty2]]] and have value2 as the result. Cant you help me write the pattern as well as explain to me what is being done in the pattern to acheive this goal. I have tried '(?<=\[{3}MyProperty1).*?(?=<\]{3})' but it does not work. Thanks in advance. Example 1: [[[MyProperty1]]] Value Example 2: [[[MyProperty1]]] Value [[[MyProperty2]]] value2 [[[MyProperty3]]] value 3

    P N 2 Replies Last reply
    0
    • F Fayu

      I am new to this whole regex thing. I have read up on it but i still need some help. I have expressions (shown below) and I want to get the values (substring). For example, in example 1 i want to search for my property ([[[MyProperty1]]]) and I want to get back 'Value'. In example 2, i want to be able to search for [[[MyProperty2]]] and have value2 as the result. Cant you help me write the pattern as well as explain to me what is being done in the pattern to acheive this goal. I have tried '(?<=\[{3}MyProperty1).*?(?=<\]{3})' but it does not work. Thanks in advance. Example 1: [[[MyProperty1]]] Value Example 2: [[[MyProperty1]]] Value [[[MyProperty2]]] value2 [[[MyProperty3]]] value 3

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

      The main problem may be that [ and ] are special characters in RegEx and need to be escaped \[ and \]. :doh: Oh, I see you did. I'm unfamiliar with <= and =<, but I just whipped this (?:\[\[\[(?'Name'.*?)\]\]\](?'Value'[^\[]*)) up with my RegexTester[^].

      modified on Friday, April 9, 2010 1:20 PM

      F 1 Reply Last reply
      0
      • P PIEBALDconsult

        The main problem may be that [ and ] are special characters in RegEx and need to be escaped \[ and \]. :doh: Oh, I see you did. I'm unfamiliar with <= and =<, but I just whipped this (?:\[\[\[(?'Name'.*?)\]\]\](?'Value'[^\[]*)) up with my RegexTester[^].

        modified on Friday, April 9, 2010 1:20 PM

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

        I used the escape sequence for the [. If I run '\[{3}MyProperty\]{3}, I get back '[[[MyProperty]]]'. So I think it may be something else. I think it may be the fact that I am trying to use lookahead/lookback.

        P 1 Reply Last reply
        0
        • F Fayu

          I used the escape sequence for the [. If I run '\[{3}MyProperty\]{3}, I get back '[[[MyProperty]]]'. So I think it may be something else. I think it may be the fact that I am trying to use lookahead/lookback.

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

          Fayu wrote:

          I am trying to use lookahead/lookback

          Why would you?

          F 1 Reply Last reply
          0
          • P PIEBALDconsult

            Fayu wrote:

            I am trying to use lookahead/lookback

            Why would you?

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

            I found an example on the web which used that to get the substring. Again, I am new to this. What would you suggest?

            P 1 Reply Last reply
            0
            • F Fayu

              I found an example on the web which used that to get the substring. Again, I am new to this. What would you suggest?

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

              What I added to my first response.

              F 1 Reply Last reply
              0
              • P PIEBALDconsult

                What I added to my first response.

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

                I didnt realize you updated your comment. The 'Value' is unknown, can I use * as a wild card?

                P 1 Reply Last reply
                0
                • F Fayu

                  I didnt realize you updated your comment. The 'Value' is unknown, can I use * as a wild card?

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

                  What I provided returns everything between the ]]] and the next [ or the end of the line.

                  1 Reply Last reply
                  0
                  • F Fayu

                    I am new to this whole regex thing. I have read up on it but i still need some help. I have expressions (shown below) and I want to get the values (substring). For example, in example 1 i want to search for my property ([[[MyProperty1]]]) and I want to get back 'Value'. In example 2, i want to be able to search for [[[MyProperty2]]] and have value2 as the result. Cant you help me write the pattern as well as explain to me what is being done in the pattern to acheive this goal. I have tried '(?<=\[{3}MyProperty1).*?(?=<\]{3})' but it does not work. Thanks in advance. Example 1: [[[MyProperty1]]] Value Example 2: [[[MyProperty1]]] Value [[[MyProperty2]]] value2 [[[MyProperty3]]] value 3

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

                    I think (\[\[\[MyProperty3\]\]\][\s{1}])(?<MyProperty3>.*?(?=\s{1}\[|$)) is enough for both of ur requirements, if i understood it clearly :| something like ...

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

                    It'd work for values like value, value2 and value 3 as well. Mark as answer if it answers ur question :)

                    modified on Friday, April 9, 2010 3:18 PM

                    P F 2 Replies Last reply
                    0
                    • N NavnathKale

                      I think (\[\[\[MyProperty3\]\]\][\s{1}])(?<MyProperty3>.*?(?=\s{1}\[|$)) is enough for both of ur requirements, if i understood it clearly :| something like ...

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

                      It'd work for values like value, value2 and value 3 as well. Mark as answer if it answers ur question :)

                      modified on Friday, April 9, 2010 3:18 PM

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

                      I prefer a more generalized technique.

                      1 Reply Last reply
                      0
                      • N NavnathKale

                        I think (\[\[\[MyProperty3\]\]\][\s{1}])(?<MyProperty3>.*?(?=\s{1}\[|$)) is enough for both of ur requirements, if i understood it clearly :| something like ...

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

                        It'd work for values like value, value2 and value 3 as well. Mark as answer if it answers ur question :)

                        modified on Friday, April 9, 2010 3:18 PM

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

                        The property name is looks like this [[[MyProperty2]]] but does not end with . So the property/value item shows up as [[[MyProperty2]]] Value2. So if there are multiple properties it will look like this: [[[MyProperty1]]] Value1 [[[MyProperty2]]] Value2 [[[MyProperty3]]] Value3. Having said that, the pattern provided did not work because I think it is looking for end tag. One other thing I want to point out is that there can be only one property which can or cannot have a space after it. My requirements: 1) Get the value of a property if only one propery is listed ([[[MyProperty1]]] value1) 2) Get the value of a property if there are more than one properies listed ([[[MyProperty1]]] value1 [[[MyProperty2]]] value2 [[[MyProperty3]]] value3 3) In the pattern, I want to pass in the property name and get back the value 4) Use VBScript.RegexExp object (which I dont think will make a difference when it comes to pattern) Thanks so much for assisting me with this.

                        N P 2 Replies Last reply
                        0
                        • F Fayu

                          The property name is looks like this [[[MyProperty2]]] but does not end with . So the property/value item shows up as [[[MyProperty2]]] Value2. So if there are multiple properties it will look like this: [[[MyProperty1]]] Value1 [[[MyProperty2]]] Value2 [[[MyProperty3]]] Value3. Having said that, the pattern provided did not work because I think it is looking for end tag. One other thing I want to point out is that there can be only one property which can or cannot have a space after it. My requirements: 1) Get the value of a property if only one propery is listed ([[[MyProperty1]]] value1) 2) Get the value of a property if there are more than one properies listed ([[[MyProperty1]]] value1 [[[MyProperty2]]] value2 [[[MyProperty3]]] value3 3) In the pattern, I want to pass in the property name and get back the value 4) Use VBScript.RegexExp object (which I dont think will make a difference when it comes to pattern) Thanks so much for assisting me with this.

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

                          I understand is not in string of properties. Its a Named capture group literal in regex used to store matched strings under that name provided inside <> This solution does worked with MyProperty1, MyProperty2 and MyProperty3. Try it urself !!! ;) Note: To get value of property, replace #### from (\[\[\[####\]\]\][\s{1}])(?<####>.*?(?=\s{1}\[|$)) to that name Example To get MyProperty3 use regex (\[\[\[MyProperty3\]\]\][\s{1}])(?<MyProperty3>.*?(?=\s{1}\[|$)) Search for Named capture groups to understand use of <> in regex. Edit: I have updated it lil bit to take care of end of line as well.

                          F 1 Reply Last reply
                          0
                          • N NavnathKale

                            I understand is not in string of properties. Its a Named capture group literal in regex used to store matched strings under that name provided inside <> This solution does worked with MyProperty1, MyProperty2 and MyProperty3. Try it urself !!! ;) Note: To get value of property, replace #### from (\[\[\[####\]\]\][\s{1}])(?<####>.*?(?=\s{1}\[|$)) to that name Example To get MyProperty3 use regex (\[\[\[MyProperty3\]\]\][\s{1}])(?<MyProperty3>.*?(?=\s{1}\[|$)) Search for Named capture groups to understand use of <> in regex. Edit: I have updated it lil bit to take care of end of line as well.

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

                            I wrote a pattern that works as well thanks to you and everyone else that helped me out. THANKS! Here is my pattern in case you want to see it (and let me know if im missing something): \[{3}MyProperty1\]{3} ([^ ]+)

                            N 1 Reply Last reply
                            0
                            • F Fayu

                              I wrote a pattern that works as well thanks to you and everyone else that helped me out. THANKS! Here is my pattern in case you want to see it (and let me know if im missing something): \[{3}MyProperty1\]{3} ([^ ]+)

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

                              Great !!! Its always better to use Named capturing groups in complicated regex patterns. But this one is ok ;) BTW what about values with spaces ?

                              F 1 Reply Last reply
                              0
                              • N NavnathKale

                                Great !!! Its always better to use Named capturing groups in complicated regex patterns. But this one is ok ;) BTW what about values with spaces ?

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

                                DOH!!! Values with spaces dont work.... how do I resolve that>

                                N 1 Reply Last reply
                                0
                                • F Fayu

                                  DOH!!! Values with spaces dont work.... how do I resolve that>

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

                                  Dont worry my solution still works !!! I guess u don;t need name captured groups, so I have removed it n here is new one

                                  (\[{3}MyProperty4\]{3}[\s{1}])(.*?(?=\s{1}\[|$))

                                  Mark as anwser !!!! :-D

                                  F 2 Replies Last reply
                                  0
                                  • F Fayu

                                    The property name is looks like this [[[MyProperty2]]] but does not end with . So the property/value item shows up as [[[MyProperty2]]] Value2. So if there are multiple properties it will look like this: [[[MyProperty1]]] Value1 [[[MyProperty2]]] Value2 [[[MyProperty3]]] Value3. Having said that, the pattern provided did not work because I think it is looking for end tag. One other thing I want to point out is that there can be only one property which can or cannot have a space after it. My requirements: 1) Get the value of a property if only one propery is listed ([[[MyProperty1]]] value1) 2) Get the value of a property if there are more than one properies listed ([[[MyProperty1]]] value1 [[[MyProperty2]]] value2 [[[MyProperty3]]] value3 3) In the pattern, I want to pass in the property name and get back the value 4) Use VBScript.RegexExp object (which I dont think will make a difference when it comes to pattern) Thanks so much for assisting me with this.

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

                                    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 P 2 Replies Last reply
                                    0
                                    • N NavnathKale

                                      Dont worry my solution still works !!! I guess u don;t need name captured groups, so I have removed it n here is new one

                                      (\[{3}MyProperty4\]{3}[\s{1}])(.*?(?=\s{1}\[|$))

                                      Mark as anwser !!!! :-D

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

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

                                      N 1 Reply Last reply
                                      0
                                      • N NavnathKale

                                        Dont worry my solution still works !!! I guess u don;t need name captured groups, so I have removed it n here is new one

                                        (\[{3}MyProperty4\]{3}[\s{1}])(.*?(?=\s{1}\[|$))

                                        Mark as anwser !!!! :-D

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

                                        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 1 Reply Last reply
                                        0
                                        • 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
                                          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