efficient regex for key/value matches











up vote
2
down vote

favorite












Let's say I have a string that represents a list of unique key/value pairs like so:



a:1;b:2;c:3;d:4


Checking whether the string contains a specific key/value pair is straight forward. But let's say I want to take advantage of the fact that the keys are unique. Is there a way to optimize the regex so that if it finds a key with a value different than the one I want, it immediately fails rather than continue scanning to the end of the string?



So, in the example above, if I want to see if b:3 is in the string, I'd want the match to fail as soon as it finds b:2. (note: an inverse search for something like b:[^3] isn't going to work for cases where they key b is missing)










share|improve this question






















  • This won't reduce complexity though, assuming position are random, the complexity are going to be the same.
    – Rocky Li
    Nov 7 at 17:15










  • Use a lookahead assertion ^(?!.*b:[^3]).*b:3
    – sln
    Nov 7 at 17:31















up vote
2
down vote

favorite












Let's say I have a string that represents a list of unique key/value pairs like so:



a:1;b:2;c:3;d:4


Checking whether the string contains a specific key/value pair is straight forward. But let's say I want to take advantage of the fact that the keys are unique. Is there a way to optimize the regex so that if it finds a key with a value different than the one I want, it immediately fails rather than continue scanning to the end of the string?



So, in the example above, if I want to see if b:3 is in the string, I'd want the match to fail as soon as it finds b:2. (note: an inverse search for something like b:[^3] isn't going to work for cases where they key b is missing)










share|improve this question






















  • This won't reduce complexity though, assuming position are random, the complexity are going to be the same.
    – Rocky Li
    Nov 7 at 17:15










  • Use a lookahead assertion ^(?!.*b:[^3]).*b:3
    – sln
    Nov 7 at 17:31













up vote
2
down vote

favorite









up vote
2
down vote

favorite











Let's say I have a string that represents a list of unique key/value pairs like so:



a:1;b:2;c:3;d:4


Checking whether the string contains a specific key/value pair is straight forward. But let's say I want to take advantage of the fact that the keys are unique. Is there a way to optimize the regex so that if it finds a key with a value different than the one I want, it immediately fails rather than continue scanning to the end of the string?



So, in the example above, if I want to see if b:3 is in the string, I'd want the match to fail as soon as it finds b:2. (note: an inverse search for something like b:[^3] isn't going to work for cases where they key b is missing)










share|improve this question













Let's say I have a string that represents a list of unique key/value pairs like so:



a:1;b:2;c:3;d:4


Checking whether the string contains a specific key/value pair is straight forward. But let's say I want to take advantage of the fact that the keys are unique. Is there a way to optimize the regex so that if it finds a key with a value different than the one I want, it immediately fails rather than continue scanning to the end of the string?



So, in the example above, if I want to see if b:3 is in the string, I'd want the match to fail as soon as it finds b:2. (note: an inverse search for something like b:[^3] isn't going to work for cases where they key b is missing)







regex






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 7 at 17:11









Dmitry B.

6,1492847




6,1492847












  • This won't reduce complexity though, assuming position are random, the complexity are going to be the same.
    – Rocky Li
    Nov 7 at 17:15










  • Use a lookahead assertion ^(?!.*b:[^3]).*b:3
    – sln
    Nov 7 at 17:31


















  • This won't reduce complexity though, assuming position are random, the complexity are going to be the same.
    – Rocky Li
    Nov 7 at 17:15










  • Use a lookahead assertion ^(?!.*b:[^3]).*b:3
    – sln
    Nov 7 at 17:31
















This won't reduce complexity though, assuming position are random, the complexity are going to be the same.
– Rocky Li
Nov 7 at 17:15




This won't reduce complexity though, assuming position are random, the complexity are going to be the same.
– Rocky Li
Nov 7 at 17:15












Use a lookahead assertion ^(?!.*b:[^3]).*b:3
– sln
Nov 7 at 17:31




Use a lookahead assertion ^(?!.*b:[^3]).*b:3
– sln
Nov 7 at 17:31












3 Answers
3






active

oldest

votes

















up vote
0
down vote













I think the fastest will be to use a two step approch. Idon't know what programming language, you're using (so this is pseudicode), but use this regex:



b:(d)


This will find the first 'b:' in the string and save the value as Group 1. Now check if the value in Group 1 is the one, you want.



For instance in JavaScript you could do:



var text = 'a:1;b:2;c:3;d:4';
var match = text.match(/b:(d)/);
if (match[1] === '3')
{
return true;
}
else
{
return false;
}


This will be a very fast approach.






share|improve this answer




























    up vote
    0
    down vote













    Something like this could work:



    import re 

    for dic in [{"a":1},{"b":2}]:
    for k,v in dic.items():
    regex = r".+?;%s:[^%d]" %(k,v)
    if re.match(regex, test): break





    share|improve this answer




























      up vote
      0
      down vote













      DEMO



      ^[^b]*b:3



      Explanation:

      Asserting that the match starts at the begging of the line with ^ guarantees that a line will contain exactly one match.
      using [^b]* expands the match to the first occurrence of 'b' and terminates the expansion, as it's not allowed to move past there.
      finally, the evaluation of b:3 occurs which either validate or invalidate the match, with no chance of re-evaluation or backtracking as the only quantifier is terminated






      share|improve this answer























        Your Answer






        StackExchange.ifUsing("editor", function () {
        StackExchange.using("externalEditor", function () {
        StackExchange.using("snippets", function () {
        StackExchange.snippets.init();
        });
        });
        }, "code-snippets");

        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "1"
        };
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function() {
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled) {
        StackExchange.using("snippets", function() {
        createEditor();
        });
        }
        else {
        createEditor();
        }
        });

        function createEditor() {
        StackExchange.prepareEditor({
        heartbeatType: 'answer',
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader: {
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        },
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        });


        }
        });














         

        draft saved


        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53194447%2fefficient-regex-for-key-value-matches%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes








        up vote
        0
        down vote













        I think the fastest will be to use a two step approch. Idon't know what programming language, you're using (so this is pseudicode), but use this regex:



        b:(d)


        This will find the first 'b:' in the string and save the value as Group 1. Now check if the value in Group 1 is the one, you want.



        For instance in JavaScript you could do:



        var text = 'a:1;b:2;c:3;d:4';
        var match = text.match(/b:(d)/);
        if (match[1] === '3')
        {
        return true;
        }
        else
        {
        return false;
        }


        This will be a very fast approach.






        share|improve this answer

























          up vote
          0
          down vote













          I think the fastest will be to use a two step approch. Idon't know what programming language, you're using (so this is pseudicode), but use this regex:



          b:(d)


          This will find the first 'b:' in the string and save the value as Group 1. Now check if the value in Group 1 is the one, you want.



          For instance in JavaScript you could do:



          var text = 'a:1;b:2;c:3;d:4';
          var match = text.match(/b:(d)/);
          if (match[1] === '3')
          {
          return true;
          }
          else
          {
          return false;
          }


          This will be a very fast approach.






          share|improve this answer























            up vote
            0
            down vote










            up vote
            0
            down vote









            I think the fastest will be to use a two step approch. Idon't know what programming language, you're using (so this is pseudicode), but use this regex:



            b:(d)


            This will find the first 'b:' in the string and save the value as Group 1. Now check if the value in Group 1 is the one, you want.



            For instance in JavaScript you could do:



            var text = 'a:1;b:2;c:3;d:4';
            var match = text.match(/b:(d)/);
            if (match[1] === '3')
            {
            return true;
            }
            else
            {
            return false;
            }


            This will be a very fast approach.






            share|improve this answer












            I think the fastest will be to use a two step approch. Idon't know what programming language, you're using (so this is pseudicode), but use this regex:



            b:(d)


            This will find the first 'b:' in the string and save the value as Group 1. Now check if the value in Group 1 is the one, you want.



            For instance in JavaScript you could do:



            var text = 'a:1;b:2;c:3;d:4';
            var match = text.match(/b:(d)/);
            if (match[1] === '3')
            {
            return true;
            }
            else
            {
            return false;
            }


            This will be a very fast approach.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 7 at 18:02









            Poul Bak

            5,26831132




            5,26831132
























                up vote
                0
                down vote













                Something like this could work:



                import re 

                for dic in [{"a":1},{"b":2}]:
                for k,v in dic.items():
                regex = r".+?;%s:[^%d]" %(k,v)
                if re.match(regex, test): break





                share|improve this answer

























                  up vote
                  0
                  down vote













                  Something like this could work:



                  import re 

                  for dic in [{"a":1},{"b":2}]:
                  for k,v in dic.items():
                  regex = r".+?;%s:[^%d]" %(k,v)
                  if re.match(regex, test): break





                  share|improve this answer























                    up vote
                    0
                    down vote










                    up vote
                    0
                    down vote









                    Something like this could work:



                    import re 

                    for dic in [{"a":1},{"b":2}]:
                    for k,v in dic.items():
                    regex = r".+?;%s:[^%d]" %(k,v)
                    if re.match(regex, test): break





                    share|improve this answer












                    Something like this could work:



                    import re 

                    for dic in [{"a":1},{"b":2}]:
                    for k,v in dic.items():
                    regex = r".+?;%s:[^%d]" %(k,v)
                    if re.match(regex, test): break






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 7 at 18:09









                    petruz

                    461311




                    461311






















                        up vote
                        0
                        down vote













                        DEMO



                        ^[^b]*b:3



                        Explanation:

                        Asserting that the match starts at the begging of the line with ^ guarantees that a line will contain exactly one match.
                        using [^b]* expands the match to the first occurrence of 'b' and terminates the expansion, as it's not allowed to move past there.
                        finally, the evaluation of b:3 occurs which either validate or invalidate the match, with no chance of re-evaluation or backtracking as the only quantifier is terminated






                        share|improve this answer



























                          up vote
                          0
                          down vote













                          DEMO



                          ^[^b]*b:3



                          Explanation:

                          Asserting that the match starts at the begging of the line with ^ guarantees that a line will contain exactly one match.
                          using [^b]* expands the match to the first occurrence of 'b' and terminates the expansion, as it's not allowed to move past there.
                          finally, the evaluation of b:3 occurs which either validate or invalidate the match, with no chance of re-evaluation or backtracking as the only quantifier is terminated






                          share|improve this answer

























                            up vote
                            0
                            down vote










                            up vote
                            0
                            down vote









                            DEMO



                            ^[^b]*b:3



                            Explanation:

                            Asserting that the match starts at the begging of the line with ^ guarantees that a line will contain exactly one match.
                            using [^b]* expands the match to the first occurrence of 'b' and terminates the expansion, as it's not allowed to move past there.
                            finally, the evaluation of b:3 occurs which either validate or invalidate the match, with no chance of re-evaluation or backtracking as the only quantifier is terminated






                            share|improve this answer














                            DEMO



                            ^[^b]*b:3



                            Explanation:

                            Asserting that the match starts at the begging of the line with ^ guarantees that a line will contain exactly one match.
                            using [^b]* expands the match to the first occurrence of 'b' and terminates the expansion, as it's not allowed to move past there.
                            finally, the evaluation of b:3 occurs which either validate or invalidate the match, with no chance of re-evaluation or backtracking as the only quantifier is terminated







                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Nov 7 at 18:16

























                            answered Nov 7 at 18:09









                            doom87er

                            38717




                            38717






























                                 

                                draft saved


                                draft discarded



















































                                 


                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53194447%2fefficient-regex-for-key-value-matches%23new-answer', 'question_page');
                                }
                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                這個網誌中的熱門文章

                                Academy of Television Arts & Sciences

                                L'Équipe

                                1995 France bombings