Match exactly strings in any order [duplicate]












0
















This question already has an answer here:




  • RegEx match open tags except XHTML self-contained tags

    35 answers




I want to match strings with the following template:



<el key1="val1" key2="val2" />


I can match them with a regex such as:



^(<els+)(?=.*key1=".*".*)(?=.*key2=".*".*)(.*/>$)


The problem is that



<el key1="val1" key2="val2" aaa />
<el key1="val1" aa key2="val2" />
<el aaa key1="val1" key2="val2" />


are also matches. I want to find ^<els+ exactly at the beginning, (/>)$ at the end and the two s+keyn=".*"s+ somewhere in between.



EDIT:
(based on comments and replies) Keys can be title, uri, text. The issue with the answers so far is the keys can be in any order, so:



<el key1="val1" key2="val2" />
<el key2="val2" key1="val1" />


are both valid.










share|improve this question















marked as duplicate by Wiktor Stribiżew, Toto regex
Users with the  regex badge can single-handedly close regex questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 19 at 14:58


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • can you elaborate more on how your keys will look like? Will it be key1 and key2 for all strings and will they be in order? key1 followed by key2?

    – Kannappan Sirchabesan
    Jan 19 at 14:14













  • @KannappanSirchabesan see my edit

    – cahenepudi
    Jan 19 at 14:29











  • Have you considered stackoverflow.com/a/1732454/2988730?

    – Mad Physicist
    Jan 19 at 14:35











  • @MadPhysicist love it, thank you

    – cahenepudi
    Jan 19 at 14:41
















0
















This question already has an answer here:




  • RegEx match open tags except XHTML self-contained tags

    35 answers




I want to match strings with the following template:



<el key1="val1" key2="val2" />


I can match them with a regex such as:



^(<els+)(?=.*key1=".*".*)(?=.*key2=".*".*)(.*/>$)


The problem is that



<el key1="val1" key2="val2" aaa />
<el key1="val1" aa key2="val2" />
<el aaa key1="val1" key2="val2" />


are also matches. I want to find ^<els+ exactly at the beginning, (/>)$ at the end and the two s+keyn=".*"s+ somewhere in between.



EDIT:
(based on comments and replies) Keys can be title, uri, text. The issue with the answers so far is the keys can be in any order, so:



<el key1="val1" key2="val2" />
<el key2="val2" key1="val1" />


are both valid.










share|improve this question















marked as duplicate by Wiktor Stribiżew, Toto regex
Users with the  regex badge can single-handedly close regex questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 19 at 14:58


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • can you elaborate more on how your keys will look like? Will it be key1 and key2 for all strings and will they be in order? key1 followed by key2?

    – Kannappan Sirchabesan
    Jan 19 at 14:14













  • @KannappanSirchabesan see my edit

    – cahenepudi
    Jan 19 at 14:29











  • Have you considered stackoverflow.com/a/1732454/2988730?

    – Mad Physicist
    Jan 19 at 14:35











  • @MadPhysicist love it, thank you

    – cahenepudi
    Jan 19 at 14:41














0












0








0









This question already has an answer here:




  • RegEx match open tags except XHTML self-contained tags

    35 answers




I want to match strings with the following template:



<el key1="val1" key2="val2" />


I can match them with a regex such as:



^(<els+)(?=.*key1=".*".*)(?=.*key2=".*".*)(.*/>$)


The problem is that



<el key1="val1" key2="val2" aaa />
<el key1="val1" aa key2="val2" />
<el aaa key1="val1" key2="val2" />


are also matches. I want to find ^<els+ exactly at the beginning, (/>)$ at the end and the two s+keyn=".*"s+ somewhere in between.



EDIT:
(based on comments and replies) Keys can be title, uri, text. The issue with the answers so far is the keys can be in any order, so:



<el key1="val1" key2="val2" />
<el key2="val2" key1="val1" />


are both valid.










share|improve this question

















This question already has an answer here:




  • RegEx match open tags except XHTML self-contained tags

    35 answers




I want to match strings with the following template:



<el key1="val1" key2="val2" />


I can match them with a regex such as:



^(<els+)(?=.*key1=".*".*)(?=.*key2=".*".*)(.*/>$)


The problem is that



<el key1="val1" key2="val2" aaa />
<el key1="val1" aa key2="val2" />
<el aaa key1="val1" key2="val2" />


are also matches. I want to find ^<els+ exactly at the beginning, (/>)$ at the end and the two s+keyn=".*"s+ somewhere in between.



EDIT:
(based on comments and replies) Keys can be title, uri, text. The issue with the answers so far is the keys can be in any order, so:



<el key1="val1" key2="val2" />
<el key2="val2" key1="val1" />


are both valid.





This question already has an answer here:




  • RegEx match open tags except XHTML self-contained tags

    35 answers








regex regex-lookarounds






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 19 at 14:29







cahenepudi

















asked Jan 19 at 13:52









cahenepudicahenepudi

113




113




marked as duplicate by Wiktor Stribiżew, Toto regex
Users with the  regex badge can single-handedly close regex questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 19 at 14:58


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by Wiktor Stribiżew, Toto regex
Users with the  regex badge can single-handedly close regex questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 19 at 14:58


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • can you elaborate more on how your keys will look like? Will it be key1 and key2 for all strings and will they be in order? key1 followed by key2?

    – Kannappan Sirchabesan
    Jan 19 at 14:14













  • @KannappanSirchabesan see my edit

    – cahenepudi
    Jan 19 at 14:29











  • Have you considered stackoverflow.com/a/1732454/2988730?

    – Mad Physicist
    Jan 19 at 14:35











  • @MadPhysicist love it, thank you

    – cahenepudi
    Jan 19 at 14:41



















  • can you elaborate more on how your keys will look like? Will it be key1 and key2 for all strings and will they be in order? key1 followed by key2?

    – Kannappan Sirchabesan
    Jan 19 at 14:14













  • @KannappanSirchabesan see my edit

    – cahenepudi
    Jan 19 at 14:29











  • Have you considered stackoverflow.com/a/1732454/2988730?

    – Mad Physicist
    Jan 19 at 14:35











  • @MadPhysicist love it, thank you

    – cahenepudi
    Jan 19 at 14:41

















can you elaborate more on how your keys will look like? Will it be key1 and key2 for all strings and will they be in order? key1 followed by key2?

– Kannappan Sirchabesan
Jan 19 at 14:14







can you elaborate more on how your keys will look like? Will it be key1 and key2 for all strings and will they be in order? key1 followed by key2?

– Kannappan Sirchabesan
Jan 19 at 14:14















@KannappanSirchabesan see my edit

– cahenepudi
Jan 19 at 14:29





@KannappanSirchabesan see my edit

– cahenepudi
Jan 19 at 14:29













Have you considered stackoverflow.com/a/1732454/2988730?

– Mad Physicist
Jan 19 at 14:35





Have you considered stackoverflow.com/a/1732454/2988730?

– Mad Physicist
Jan 19 at 14:35













@MadPhysicist love it, thank you

– cahenepudi
Jan 19 at 14:41





@MadPhysicist love it, thank you

– cahenepudi
Jan 19 at 14:41












2 Answers
2






active

oldest

votes


















0














You should really use an XML parser of your language, because the things that you don't want to match are all invalid XML tags, so they can be ruled out easily. You just need to check for the tag name being el, and having only 2 attributes key1 and key2.



If you insist on regex, here is one that should work:



^<els+key1=".*?"s+key2=".*?"s+/>$


The main difference between this and your attempt is that your attempt uses too many lookaheads. Why are you using lookaheads? Normal matching should be fine. You lookahead for key1=".*?" and then match .* greedily, which seems quite redundant. Therefore, I deleted the lookaheads and .*s. And it only matches the string you want.



If you also want to match strings where key2 appears before key1, the regex becomes much longer:



^<els+(?:key1=".*?"s+key2=".*?"|key2=".*?"s+key1=".*?")s+/>$


This is why I said you should use a XML parser.






share|improve this answer
























  • Is it not feasible with lookarounds in the way I was doing? Your second answer looks like what I was looking for, but if it becomes too long I will take your suggestion for the XML parser

    – cahenepudi
    Jan 19 at 14:31











  • It is feasible, but it'll even longer. You'd have to lookahead for something, then match that something. This is quite redundant, because why not just straight up say "match something"? which is what my regex does. @cahenepudi

    – Sweeper
    Jan 19 at 14:34











  • @cahenepudi If you think an answers your question, please consider accepting it by clicking on that checkmark!

    – Sweeper
    Jan 19 at 14:35











  • Accepting after reading why not to use regexps for parsing XML, although this solution in my case would work.

    – cahenepudi
    Jan 19 at 14:42



















0














Here is a regex that could work



^(<el)s+(S+=".*")s+(S+=".*")s+(/>$)





share|improve this answer
































    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    You should really use an XML parser of your language, because the things that you don't want to match are all invalid XML tags, so they can be ruled out easily. You just need to check for the tag name being el, and having only 2 attributes key1 and key2.



    If you insist on regex, here is one that should work:



    ^<els+key1=".*?"s+key2=".*?"s+/>$


    The main difference between this and your attempt is that your attempt uses too many lookaheads. Why are you using lookaheads? Normal matching should be fine. You lookahead for key1=".*?" and then match .* greedily, which seems quite redundant. Therefore, I deleted the lookaheads and .*s. And it only matches the string you want.



    If you also want to match strings where key2 appears before key1, the regex becomes much longer:



    ^<els+(?:key1=".*?"s+key2=".*?"|key2=".*?"s+key1=".*?")s+/>$


    This is why I said you should use a XML parser.






    share|improve this answer
























    • Is it not feasible with lookarounds in the way I was doing? Your second answer looks like what I was looking for, but if it becomes too long I will take your suggestion for the XML parser

      – cahenepudi
      Jan 19 at 14:31











    • It is feasible, but it'll even longer. You'd have to lookahead for something, then match that something. This is quite redundant, because why not just straight up say "match something"? which is what my regex does. @cahenepudi

      – Sweeper
      Jan 19 at 14:34











    • @cahenepudi If you think an answers your question, please consider accepting it by clicking on that checkmark!

      – Sweeper
      Jan 19 at 14:35











    • Accepting after reading why not to use regexps for parsing XML, although this solution in my case would work.

      – cahenepudi
      Jan 19 at 14:42
















    0














    You should really use an XML parser of your language, because the things that you don't want to match are all invalid XML tags, so they can be ruled out easily. You just need to check for the tag name being el, and having only 2 attributes key1 and key2.



    If you insist on regex, here is one that should work:



    ^<els+key1=".*?"s+key2=".*?"s+/>$


    The main difference between this and your attempt is that your attempt uses too many lookaheads. Why are you using lookaheads? Normal matching should be fine. You lookahead for key1=".*?" and then match .* greedily, which seems quite redundant. Therefore, I deleted the lookaheads and .*s. And it only matches the string you want.



    If you also want to match strings where key2 appears before key1, the regex becomes much longer:



    ^<els+(?:key1=".*?"s+key2=".*?"|key2=".*?"s+key1=".*?")s+/>$


    This is why I said you should use a XML parser.






    share|improve this answer
























    • Is it not feasible with lookarounds in the way I was doing? Your second answer looks like what I was looking for, but if it becomes too long I will take your suggestion for the XML parser

      – cahenepudi
      Jan 19 at 14:31











    • It is feasible, but it'll even longer. You'd have to lookahead for something, then match that something. This is quite redundant, because why not just straight up say "match something"? which is what my regex does. @cahenepudi

      – Sweeper
      Jan 19 at 14:34











    • @cahenepudi If you think an answers your question, please consider accepting it by clicking on that checkmark!

      – Sweeper
      Jan 19 at 14:35











    • Accepting after reading why not to use regexps for parsing XML, although this solution in my case would work.

      – cahenepudi
      Jan 19 at 14:42














    0












    0








    0







    You should really use an XML parser of your language, because the things that you don't want to match are all invalid XML tags, so they can be ruled out easily. You just need to check for the tag name being el, and having only 2 attributes key1 and key2.



    If you insist on regex, here is one that should work:



    ^<els+key1=".*?"s+key2=".*?"s+/>$


    The main difference between this and your attempt is that your attempt uses too many lookaheads. Why are you using lookaheads? Normal matching should be fine. You lookahead for key1=".*?" and then match .* greedily, which seems quite redundant. Therefore, I deleted the lookaheads and .*s. And it only matches the string you want.



    If you also want to match strings where key2 appears before key1, the regex becomes much longer:



    ^<els+(?:key1=".*?"s+key2=".*?"|key2=".*?"s+key1=".*?")s+/>$


    This is why I said you should use a XML parser.






    share|improve this answer













    You should really use an XML parser of your language, because the things that you don't want to match are all invalid XML tags, so they can be ruled out easily. You just need to check for the tag name being el, and having only 2 attributes key1 and key2.



    If you insist on regex, here is one that should work:



    ^<els+key1=".*?"s+key2=".*?"s+/>$


    The main difference between this and your attempt is that your attempt uses too many lookaheads. Why are you using lookaheads? Normal matching should be fine. You lookahead for key1=".*?" and then match .* greedily, which seems quite redundant. Therefore, I deleted the lookaheads and .*s. And it only matches the string you want.



    If you also want to match strings where key2 appears before key1, the regex becomes much longer:



    ^<els+(?:key1=".*?"s+key2=".*?"|key2=".*?"s+key1=".*?")s+/>$


    This is why I said you should use a XML parser.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Jan 19 at 14:24









    SweeperSweeper

    66.7k1073139




    66.7k1073139













    • Is it not feasible with lookarounds in the way I was doing? Your second answer looks like what I was looking for, but if it becomes too long I will take your suggestion for the XML parser

      – cahenepudi
      Jan 19 at 14:31











    • It is feasible, but it'll even longer. You'd have to lookahead for something, then match that something. This is quite redundant, because why not just straight up say "match something"? which is what my regex does. @cahenepudi

      – Sweeper
      Jan 19 at 14:34











    • @cahenepudi If you think an answers your question, please consider accepting it by clicking on that checkmark!

      – Sweeper
      Jan 19 at 14:35











    • Accepting after reading why not to use regexps for parsing XML, although this solution in my case would work.

      – cahenepudi
      Jan 19 at 14:42



















    • Is it not feasible with lookarounds in the way I was doing? Your second answer looks like what I was looking for, but if it becomes too long I will take your suggestion for the XML parser

      – cahenepudi
      Jan 19 at 14:31











    • It is feasible, but it'll even longer. You'd have to lookahead for something, then match that something. This is quite redundant, because why not just straight up say "match something"? which is what my regex does. @cahenepudi

      – Sweeper
      Jan 19 at 14:34











    • @cahenepudi If you think an answers your question, please consider accepting it by clicking on that checkmark!

      – Sweeper
      Jan 19 at 14:35











    • Accepting after reading why not to use regexps for parsing XML, although this solution in my case would work.

      – cahenepudi
      Jan 19 at 14:42

















    Is it not feasible with lookarounds in the way I was doing? Your second answer looks like what I was looking for, but if it becomes too long I will take your suggestion for the XML parser

    – cahenepudi
    Jan 19 at 14:31





    Is it not feasible with lookarounds in the way I was doing? Your second answer looks like what I was looking for, but if it becomes too long I will take your suggestion for the XML parser

    – cahenepudi
    Jan 19 at 14:31













    It is feasible, but it'll even longer. You'd have to lookahead for something, then match that something. This is quite redundant, because why not just straight up say "match something"? which is what my regex does. @cahenepudi

    – Sweeper
    Jan 19 at 14:34





    It is feasible, but it'll even longer. You'd have to lookahead for something, then match that something. This is quite redundant, because why not just straight up say "match something"? which is what my regex does. @cahenepudi

    – Sweeper
    Jan 19 at 14:34













    @cahenepudi If you think an answers your question, please consider accepting it by clicking on that checkmark!

    – Sweeper
    Jan 19 at 14:35





    @cahenepudi If you think an answers your question, please consider accepting it by clicking on that checkmark!

    – Sweeper
    Jan 19 at 14:35













    Accepting after reading why not to use regexps for parsing XML, although this solution in my case would work.

    – cahenepudi
    Jan 19 at 14:42





    Accepting after reading why not to use regexps for parsing XML, although this solution in my case would work.

    – cahenepudi
    Jan 19 at 14:42













    0














    Here is a regex that could work



    ^(<el)s+(S+=".*")s+(S+=".*")s+(/>$)





    share|improve this answer






























      0














      Here is a regex that could work



      ^(<el)s+(S+=".*")s+(S+=".*")s+(/>$)





      share|improve this answer




























        0












        0








        0







        Here is a regex that could work



        ^(<el)s+(S+=".*")s+(S+=".*")s+(/>$)





        share|improve this answer















        Here is a regex that could work



        ^(<el)s+(S+=".*")s+(S+=".*")s+(/>$)






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Jan 19 at 14:33

























        answered Jan 19 at 14:21









        Kannappan SirchabesanKannappan Sirchabesan

        562413




        562413















            Popular posts from this blog

            Callistus III

            Ostreoida

            Plistias Cous