How to get URL inside @import statement by regexp












2















I use this Reguler Expression to get @import statements of CSS Text if there is. This works successfully, for example:



RegExp:
(?:^|s)?(?:@import)(?:s)(?:url)?(?:(?:(?:()(["'])?(?:[^"')]+)1(?:))|(["'])(?:.+)2)(?:[A-Zs])*)+(?:;)

Input:
@import url('https://meyerweb.com/eric/tools/css/reset/reset200802.css');@import url('http://opdetect.com/x/1-2-2.css');:root{--blue:#007bff;--indigo:#6610f2;--purple:#6f42c1;--pink:#e83e8c;--red:#dc3545;--orange:#fd7e14;--yellow:#ffc107;--green:#28a745;--teal:#20c997;--cyan:#17a2b8;--white:#fff;--gray:#6c757d;--gray-dark:#343a40;--primary:#007bff;--secondary:#6c757d;--success:#28a745;--info:#17a2b8;--warning:#ffc107;--danger:#dc3545;--light:#f8f9fa;--dark:#343a40;--breakpoint-xs:0;--breakpoint-sm:576px;--breakpoint-md:768px;--breakpoint-lg:992px;--breakpoint-xl:1200px;--font-family-sans-serif:-apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,"Helvetica Neue",Arial,sans-serif,"Apple Color Emoji","Segoe UI Emoji","Segoe UI Symbol","Noto Color Emoji";--font-family-monospace:SFMono-Regular,Menlo,Monaco,Consolas,"Liberation Mono","Courier New",monospace}

Result:
@import url('https://meyerweb.com/eric/tools/css/reset/reset200802.css');
@import url('http://opdetect.com/x/1-2-2.css');


But, I want to result this like;



Expected Result:
https://meyerweb.com/eric/tools/css/reset/reset200802.css
http://opdetect.com/x/1-2-2.css


How can I do that?










share|improve this question



























    2















    I use this Reguler Expression to get @import statements of CSS Text if there is. This works successfully, for example:



    RegExp:
    (?:^|s)?(?:@import)(?:s)(?:url)?(?:(?:(?:()(["'])?(?:[^"')]+)1(?:))|(["'])(?:.+)2)(?:[A-Zs])*)+(?:;)

    Input:
    @import url('https://meyerweb.com/eric/tools/css/reset/reset200802.css');@import url('http://opdetect.com/x/1-2-2.css');:root{--blue:#007bff;--indigo:#6610f2;--purple:#6f42c1;--pink:#e83e8c;--red:#dc3545;--orange:#fd7e14;--yellow:#ffc107;--green:#28a745;--teal:#20c997;--cyan:#17a2b8;--white:#fff;--gray:#6c757d;--gray-dark:#343a40;--primary:#007bff;--secondary:#6c757d;--success:#28a745;--info:#17a2b8;--warning:#ffc107;--danger:#dc3545;--light:#f8f9fa;--dark:#343a40;--breakpoint-xs:0;--breakpoint-sm:576px;--breakpoint-md:768px;--breakpoint-lg:992px;--breakpoint-xl:1200px;--font-family-sans-serif:-apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,"Helvetica Neue",Arial,sans-serif,"Apple Color Emoji","Segoe UI Emoji","Segoe UI Symbol","Noto Color Emoji";--font-family-monospace:SFMono-Regular,Menlo,Monaco,Consolas,"Liberation Mono","Courier New",monospace}

    Result:
    @import url('https://meyerweb.com/eric/tools/css/reset/reset200802.css');
    @import url('http://opdetect.com/x/1-2-2.css');


    But, I want to result this like;



    Expected Result:
    https://meyerweb.com/eric/tools/css/reset/reset200802.css
    http://opdetect.com/x/1-2-2.css


    How can I do that?










    share|improve this question

























      2












      2








      2


      0






      I use this Reguler Expression to get @import statements of CSS Text if there is. This works successfully, for example:



      RegExp:
      (?:^|s)?(?:@import)(?:s)(?:url)?(?:(?:(?:()(["'])?(?:[^"')]+)1(?:))|(["'])(?:.+)2)(?:[A-Zs])*)+(?:;)

      Input:
      @import url('https://meyerweb.com/eric/tools/css/reset/reset200802.css');@import url('http://opdetect.com/x/1-2-2.css');:root{--blue:#007bff;--indigo:#6610f2;--purple:#6f42c1;--pink:#e83e8c;--red:#dc3545;--orange:#fd7e14;--yellow:#ffc107;--green:#28a745;--teal:#20c997;--cyan:#17a2b8;--white:#fff;--gray:#6c757d;--gray-dark:#343a40;--primary:#007bff;--secondary:#6c757d;--success:#28a745;--info:#17a2b8;--warning:#ffc107;--danger:#dc3545;--light:#f8f9fa;--dark:#343a40;--breakpoint-xs:0;--breakpoint-sm:576px;--breakpoint-md:768px;--breakpoint-lg:992px;--breakpoint-xl:1200px;--font-family-sans-serif:-apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,"Helvetica Neue",Arial,sans-serif,"Apple Color Emoji","Segoe UI Emoji","Segoe UI Symbol","Noto Color Emoji";--font-family-monospace:SFMono-Regular,Menlo,Monaco,Consolas,"Liberation Mono","Courier New",monospace}

      Result:
      @import url('https://meyerweb.com/eric/tools/css/reset/reset200802.css');
      @import url('http://opdetect.com/x/1-2-2.css');


      But, I want to result this like;



      Expected Result:
      https://meyerweb.com/eric/tools/css/reset/reset200802.css
      http://opdetect.com/x/1-2-2.css


      How can I do that?










      share|improve this question














      I use this Reguler Expression to get @import statements of CSS Text if there is. This works successfully, for example:



      RegExp:
      (?:^|s)?(?:@import)(?:s)(?:url)?(?:(?:(?:()(["'])?(?:[^"')]+)1(?:))|(["'])(?:.+)2)(?:[A-Zs])*)+(?:;)

      Input:
      @import url('https://meyerweb.com/eric/tools/css/reset/reset200802.css');@import url('http://opdetect.com/x/1-2-2.css');:root{--blue:#007bff;--indigo:#6610f2;--purple:#6f42c1;--pink:#e83e8c;--red:#dc3545;--orange:#fd7e14;--yellow:#ffc107;--green:#28a745;--teal:#20c997;--cyan:#17a2b8;--white:#fff;--gray:#6c757d;--gray-dark:#343a40;--primary:#007bff;--secondary:#6c757d;--success:#28a745;--info:#17a2b8;--warning:#ffc107;--danger:#dc3545;--light:#f8f9fa;--dark:#343a40;--breakpoint-xs:0;--breakpoint-sm:576px;--breakpoint-md:768px;--breakpoint-lg:992px;--breakpoint-xl:1200px;--font-family-sans-serif:-apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,"Helvetica Neue",Arial,sans-serif,"Apple Color Emoji","Segoe UI Emoji","Segoe UI Symbol","Noto Color Emoji";--font-family-monospace:SFMono-Regular,Menlo,Monaco,Consolas,"Liberation Mono","Courier New",monospace}

      Result:
      @import url('https://meyerweb.com/eric/tools/css/reset/reset200802.css');
      @import url('http://opdetect.com/x/1-2-2.css');


      But, I want to result this like;



      Expected Result:
      https://meyerweb.com/eric/tools/css/reset/reset200802.css
      http://opdetect.com/x/1-2-2.css


      How can I do that?







      javascript css regex






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 16 '18 at 7:54









      n3pixowen3pixowe

      747




      747
























          3 Answers
          3






          active

          oldest

          votes


















          1














          You can use this regex, which is based on yours:



          (?:^|s)?(?:@import)(?:s)(?:url)?(?:(?:(?:()(["'])?(?:https?:)?([^"')]+)1(?:))|(["'])(?:.+)2)(?:[A-Zs])*)+(?:;)


          I have changed a non capturing Group to a capturing Group, and created a new non capturing Group containing 'https?:'.



          You need to set the 'global' flag.



          Your desired result will be in Group 2.



          Edit, based on your changed desired result ('http:' is now included in the match):



          (?:^|s)?(?:@import)(?:s)(?:url)?(?:(?:(?:()(["'])?([^"')]+)1(?:))|(["'])(?:.+)2)(?:[A-Zs])*)+(?:;)


          Edit (found a mistake, changed the back reference):



          (?:^|s)?(?:@import)(?:s)(?:url)?(?:(?:(?:()(["'])?([^"')]+)1(?:))|(["'])(.+)3)(?:[A-Zs])*)+(?:;)


          Your result will be in Group 2 OR Group 4.



          How to use:



          var input = 'your css';
          var regex = / (?:^|s)?(?:@import)(?:s)(?:url)?(?:(?:(?:()(["'])?([^"')]+)1(?:))|(["'])(.+)3)(?:[A-Zs])*)+(?:;)/g;
          var matches = regex.exec(input);
          for (var i = 0; i < matches.length; i++)
          {
          var output = matches[i][2] + matches[i][4]; // one will be blank
          Console.WriteLine(output);
          }





          share|improve this answer

































            1














            This may help you...
            @import (url("?)?(url()?(")?(.*?)(?(1)")+(?(2)))+(?(3)");



            Check out Here:
            https://regex101.com/r/Dm2M36/1






            share|improve this answer
























            • Is this works for all different notations of import statement?

              – n3pixowe
              Nov 16 '18 at 8:07











            • The regex is only written for @import url('meyerweb.com/eric/tools/css/reset/reset200802.css'); kind of import queries.

              – OneJeet
              Nov 16 '18 at 8:23



















            0














            Maybe this can help you.



            (https?://.+.css)(?=');@)|(http://.+.css)


            And you have your urls in group 1 and 2






            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',
              autoActivateHeartbeat: false,
              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%2f53333580%2fhow-to-get-url-inside-import-statement-by-regexp%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









              1














              You can use this regex, which is based on yours:



              (?:^|s)?(?:@import)(?:s)(?:url)?(?:(?:(?:()(["'])?(?:https?:)?([^"')]+)1(?:))|(["'])(?:.+)2)(?:[A-Zs])*)+(?:;)


              I have changed a non capturing Group to a capturing Group, and created a new non capturing Group containing 'https?:'.



              You need to set the 'global' flag.



              Your desired result will be in Group 2.



              Edit, based on your changed desired result ('http:' is now included in the match):



              (?:^|s)?(?:@import)(?:s)(?:url)?(?:(?:(?:()(["'])?([^"')]+)1(?:))|(["'])(?:.+)2)(?:[A-Zs])*)+(?:;)


              Edit (found a mistake, changed the back reference):



              (?:^|s)?(?:@import)(?:s)(?:url)?(?:(?:(?:()(["'])?([^"')]+)1(?:))|(["'])(.+)3)(?:[A-Zs])*)+(?:;)


              Your result will be in Group 2 OR Group 4.



              How to use:



              var input = 'your css';
              var regex = / (?:^|s)?(?:@import)(?:s)(?:url)?(?:(?:(?:()(["'])?([^"')]+)1(?:))|(["'])(.+)3)(?:[A-Zs])*)+(?:;)/g;
              var matches = regex.exec(input);
              for (var i = 0; i < matches.length; i++)
              {
              var output = matches[i][2] + matches[i][4]; // one will be blank
              Console.WriteLine(output);
              }





              share|improve this answer






























                1














                You can use this regex, which is based on yours:



                (?:^|s)?(?:@import)(?:s)(?:url)?(?:(?:(?:()(["'])?(?:https?:)?([^"')]+)1(?:))|(["'])(?:.+)2)(?:[A-Zs])*)+(?:;)


                I have changed a non capturing Group to a capturing Group, and created a new non capturing Group containing 'https?:'.



                You need to set the 'global' flag.



                Your desired result will be in Group 2.



                Edit, based on your changed desired result ('http:' is now included in the match):



                (?:^|s)?(?:@import)(?:s)(?:url)?(?:(?:(?:()(["'])?([^"')]+)1(?:))|(["'])(?:.+)2)(?:[A-Zs])*)+(?:;)


                Edit (found a mistake, changed the back reference):



                (?:^|s)?(?:@import)(?:s)(?:url)?(?:(?:(?:()(["'])?([^"')]+)1(?:))|(["'])(.+)3)(?:[A-Zs])*)+(?:;)


                Your result will be in Group 2 OR Group 4.



                How to use:



                var input = 'your css';
                var regex = / (?:^|s)?(?:@import)(?:s)(?:url)?(?:(?:(?:()(["'])?([^"')]+)1(?:))|(["'])(.+)3)(?:[A-Zs])*)+(?:;)/g;
                var matches = regex.exec(input);
                for (var i = 0; i < matches.length; i++)
                {
                var output = matches[i][2] + matches[i][4]; // one will be blank
                Console.WriteLine(output);
                }





                share|improve this answer




























                  1












                  1








                  1







                  You can use this regex, which is based on yours:



                  (?:^|s)?(?:@import)(?:s)(?:url)?(?:(?:(?:()(["'])?(?:https?:)?([^"')]+)1(?:))|(["'])(?:.+)2)(?:[A-Zs])*)+(?:;)


                  I have changed a non capturing Group to a capturing Group, and created a new non capturing Group containing 'https?:'.



                  You need to set the 'global' flag.



                  Your desired result will be in Group 2.



                  Edit, based on your changed desired result ('http:' is now included in the match):



                  (?:^|s)?(?:@import)(?:s)(?:url)?(?:(?:(?:()(["'])?([^"')]+)1(?:))|(["'])(?:.+)2)(?:[A-Zs])*)+(?:;)


                  Edit (found a mistake, changed the back reference):



                  (?:^|s)?(?:@import)(?:s)(?:url)?(?:(?:(?:()(["'])?([^"')]+)1(?:))|(["'])(.+)3)(?:[A-Zs])*)+(?:;)


                  Your result will be in Group 2 OR Group 4.



                  How to use:



                  var input = 'your css';
                  var regex = / (?:^|s)?(?:@import)(?:s)(?:url)?(?:(?:(?:()(["'])?([^"')]+)1(?:))|(["'])(.+)3)(?:[A-Zs])*)+(?:;)/g;
                  var matches = regex.exec(input);
                  for (var i = 0; i < matches.length; i++)
                  {
                  var output = matches[i][2] + matches[i][4]; // one will be blank
                  Console.WriteLine(output);
                  }





                  share|improve this answer















                  You can use this regex, which is based on yours:



                  (?:^|s)?(?:@import)(?:s)(?:url)?(?:(?:(?:()(["'])?(?:https?:)?([^"')]+)1(?:))|(["'])(?:.+)2)(?:[A-Zs])*)+(?:;)


                  I have changed a non capturing Group to a capturing Group, and created a new non capturing Group containing 'https?:'.



                  You need to set the 'global' flag.



                  Your desired result will be in Group 2.



                  Edit, based on your changed desired result ('http:' is now included in the match):



                  (?:^|s)?(?:@import)(?:s)(?:url)?(?:(?:(?:()(["'])?([^"')]+)1(?:))|(["'])(?:.+)2)(?:[A-Zs])*)+(?:;)


                  Edit (found a mistake, changed the back reference):



                  (?:^|s)?(?:@import)(?:s)(?:url)?(?:(?:(?:()(["'])?([^"')]+)1(?:))|(["'])(.+)3)(?:[A-Zs])*)+(?:;)


                  Your result will be in Group 2 OR Group 4.



                  How to use:



                  var input = 'your css';
                  var regex = / (?:^|s)?(?:@import)(?:s)(?:url)?(?:(?:(?:()(["'])?([^"')]+)1(?:))|(["'])(.+)3)(?:[A-Zs])*)+(?:;)/g;
                  var matches = regex.exec(input);
                  for (var i = 0; i < matches.length; i++)
                  {
                  var output = matches[i][2] + matches[i][4]; // one will be blank
                  Console.WriteLine(output);
                  }






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 16 '18 at 9:01

























                  answered Nov 16 '18 at 8:11









                  Poul BakPoul Bak

                  5,46831232




                  5,46831232

























                      1














                      This may help you...
                      @import (url("?)?(url()?(")?(.*?)(?(1)")+(?(2)))+(?(3)");



                      Check out Here:
                      https://regex101.com/r/Dm2M36/1






                      share|improve this answer
























                      • Is this works for all different notations of import statement?

                        – n3pixowe
                        Nov 16 '18 at 8:07











                      • The regex is only written for @import url('meyerweb.com/eric/tools/css/reset/reset200802.css'); kind of import queries.

                        – OneJeet
                        Nov 16 '18 at 8:23
















                      1














                      This may help you...
                      @import (url("?)?(url()?(")?(.*?)(?(1)")+(?(2)))+(?(3)");



                      Check out Here:
                      https://regex101.com/r/Dm2M36/1






                      share|improve this answer
























                      • Is this works for all different notations of import statement?

                        – n3pixowe
                        Nov 16 '18 at 8:07











                      • The regex is only written for @import url('meyerweb.com/eric/tools/css/reset/reset200802.css'); kind of import queries.

                        – OneJeet
                        Nov 16 '18 at 8:23














                      1












                      1








                      1







                      This may help you...
                      @import (url("?)?(url()?(")?(.*?)(?(1)")+(?(2)))+(?(3)");



                      Check out Here:
                      https://regex101.com/r/Dm2M36/1






                      share|improve this answer













                      This may help you...
                      @import (url("?)?(url()?(")?(.*?)(?(1)")+(?(2)))+(?(3)");



                      Check out Here:
                      https://regex101.com/r/Dm2M36/1







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Nov 16 '18 at 8:05









                      OneJeetOneJeet

                      962310




                      962310













                      • Is this works for all different notations of import statement?

                        – n3pixowe
                        Nov 16 '18 at 8:07











                      • The regex is only written for @import url('meyerweb.com/eric/tools/css/reset/reset200802.css'); kind of import queries.

                        – OneJeet
                        Nov 16 '18 at 8:23



















                      • Is this works for all different notations of import statement?

                        – n3pixowe
                        Nov 16 '18 at 8:07











                      • The regex is only written for @import url('meyerweb.com/eric/tools/css/reset/reset200802.css'); kind of import queries.

                        – OneJeet
                        Nov 16 '18 at 8:23

















                      Is this works for all different notations of import statement?

                      – n3pixowe
                      Nov 16 '18 at 8:07





                      Is this works for all different notations of import statement?

                      – n3pixowe
                      Nov 16 '18 at 8:07













                      The regex is only written for @import url('meyerweb.com/eric/tools/css/reset/reset200802.css'); kind of import queries.

                      – OneJeet
                      Nov 16 '18 at 8:23





                      The regex is only written for @import url('meyerweb.com/eric/tools/css/reset/reset200802.css'); kind of import queries.

                      – OneJeet
                      Nov 16 '18 at 8:23











                      0














                      Maybe this can help you.



                      (https?://.+.css)(?=');@)|(http://.+.css)


                      And you have your urls in group 1 and 2






                      share|improve this answer




























                        0














                        Maybe this can help you.



                        (https?://.+.css)(?=');@)|(http://.+.css)


                        And you have your urls in group 1 and 2






                        share|improve this answer


























                          0












                          0








                          0







                          Maybe this can help you.



                          (https?://.+.css)(?=');@)|(http://.+.css)


                          And you have your urls in group 1 and 2






                          share|improve this answer













                          Maybe this can help you.



                          (https?://.+.css)(?=');@)|(http://.+.css)


                          And you have your urls in group 1 and 2







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 16 '18 at 14:16









                          DanielDaniel

                          38115




                          38115






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53333580%2fhow-to-get-url-inside-import-statement-by-regexp%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







                              這個網誌中的熱門文章

                              Xamarin.form Move up view when keyboard appear

                              Post-Redirect-Get with Spring WebFlux and Thymeleaf

                              Anylogic : not able to use stopDelay()