Regular expression - starting and ending with a character string












47















I would like to write a regular expression that starts with the string "wp" and ends with the string "php" to locate a file in a directory. How do I do it?



Example file: wp-comments-post.php










share|improve this question


















  • 1





    There ia a good tutorial here: regular-expressions.info/tutorial.html

    – Casimir et Hippolyte
    Aug 2 '13 at 19:02











  • Why regex? Wouldn't you rather use globbing when looking for files?

    – Tim Pietzcker
    Aug 2 '13 at 19:04











  • How would you do it using globbing?

    – Ken Shoufer
    Aug 2 '13 at 20:07
















47















I would like to write a regular expression that starts with the string "wp" and ends with the string "php" to locate a file in a directory. How do I do it?



Example file: wp-comments-post.php










share|improve this question


















  • 1





    There ia a good tutorial here: regular-expressions.info/tutorial.html

    – Casimir et Hippolyte
    Aug 2 '13 at 19:02











  • Why regex? Wouldn't you rather use globbing when looking for files?

    – Tim Pietzcker
    Aug 2 '13 at 19:04











  • How would you do it using globbing?

    – Ken Shoufer
    Aug 2 '13 at 20:07














47












47








47


9






I would like to write a regular expression that starts with the string "wp" and ends with the string "php" to locate a file in a directory. How do I do it?



Example file: wp-comments-post.php










share|improve this question














I would like to write a regular expression that starts with the string "wp" and ends with the string "php" to locate a file in a directory. How do I do it?



Example file: wp-comments-post.php







regex string






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Aug 2 '13 at 19:01









Ken ShouferKen Shoufer

4223613




4223613








  • 1





    There ia a good tutorial here: regular-expressions.info/tutorial.html

    – Casimir et Hippolyte
    Aug 2 '13 at 19:02











  • Why regex? Wouldn't you rather use globbing when looking for files?

    – Tim Pietzcker
    Aug 2 '13 at 19:04











  • How would you do it using globbing?

    – Ken Shoufer
    Aug 2 '13 at 20:07














  • 1





    There ia a good tutorial here: regular-expressions.info/tutorial.html

    – Casimir et Hippolyte
    Aug 2 '13 at 19:02











  • Why regex? Wouldn't you rather use globbing when looking for files?

    – Tim Pietzcker
    Aug 2 '13 at 19:04











  • How would you do it using globbing?

    – Ken Shoufer
    Aug 2 '13 at 20:07








1




1





There ia a good tutorial here: regular-expressions.info/tutorial.html

– Casimir et Hippolyte
Aug 2 '13 at 19:02





There ia a good tutorial here: regular-expressions.info/tutorial.html

– Casimir et Hippolyte
Aug 2 '13 at 19:02













Why regex? Wouldn't you rather use globbing when looking for files?

– Tim Pietzcker
Aug 2 '13 at 19:04





Why regex? Wouldn't you rather use globbing when looking for files?

– Tim Pietzcker
Aug 2 '13 at 19:04













How would you do it using globbing?

– Ken Shoufer
Aug 2 '13 at 20:07





How would you do it using globbing?

– Ken Shoufer
Aug 2 '13 at 20:07












3 Answers
3






active

oldest

votes


















81














This should do it for you ^wp.*php$



Matches



wp-comments-post.php
wp.something.php
wp.php


Doesn't match



something-wp.php
wp.php.txt





share|improve this answer































    33














    ^wp.*.php$ Should do the trick.



    The .* means "any character, repeated 0 or more times". The next . is escaped because it's a special character, and you want a literal period (".php"). Don't forget that if you're typing this in as a literal string in something like C#, Java, etc., you need to escape the backslash because it's a special character in many literal strings.






    share|improve this answer
























    • This should also work: ^wp.+.php$ The only difference is the '+', which makes the dot "greedy".

      – Adam Howell
      Apr 8 '16 at 3:35








    • 4





      @AdamHowell + and * are both greedy, although greediness doesn't make a difference in this case. The difference is that * matches zero or more characters, and the + matches one or more characters. So if someone wanted to match "wp.php", they should use *; if they specifically wanted to not match that, they should use +.

      – Michelle
      Apr 11 '16 at 10:34



















    4














    Example:
    ajshdjashdjashdlasdlhdlSTARTasdasdsdaasdENDaknsdklansdlknaldknaaklsdn



    1) STARTw*END
    return: STARTasdasdsdaasdEND - will give you words between START and END



    2) STARTd*END
    return: START12121212END - will give you numbers between START and END



    3) STARTd*_d*END
    return: START1212_1212END - will give you numbers between START and END having _






    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%2f18024298%2fregular-expression-starting-and-ending-with-a-character-string%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









      81














      This should do it for you ^wp.*php$



      Matches



      wp-comments-post.php
      wp.something.php
      wp.php


      Doesn't match



      something-wp.php
      wp.php.txt





      share|improve this answer




























        81














        This should do it for you ^wp.*php$



        Matches



        wp-comments-post.php
        wp.something.php
        wp.php


        Doesn't match



        something-wp.php
        wp.php.txt





        share|improve this answer


























          81












          81








          81







          This should do it for you ^wp.*php$



          Matches



          wp-comments-post.php
          wp.something.php
          wp.php


          Doesn't match



          something-wp.php
          wp.php.txt





          share|improve this answer













          This should do it for you ^wp.*php$



          Matches



          wp-comments-post.php
          wp.something.php
          wp.php


          Doesn't match



          something-wp.php
          wp.php.txt






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Aug 2 '13 at 19:04









          SyonSyon

          5,73643037




          5,73643037

























              33














              ^wp.*.php$ Should do the trick.



              The .* means "any character, repeated 0 or more times". The next . is escaped because it's a special character, and you want a literal period (".php"). Don't forget that if you're typing this in as a literal string in something like C#, Java, etc., you need to escape the backslash because it's a special character in many literal strings.






              share|improve this answer
























              • This should also work: ^wp.+.php$ The only difference is the '+', which makes the dot "greedy".

                – Adam Howell
                Apr 8 '16 at 3:35








              • 4





                @AdamHowell + and * are both greedy, although greediness doesn't make a difference in this case. The difference is that * matches zero or more characters, and the + matches one or more characters. So if someone wanted to match "wp.php", they should use *; if they specifically wanted to not match that, they should use +.

                – Michelle
                Apr 11 '16 at 10:34
















              33














              ^wp.*.php$ Should do the trick.



              The .* means "any character, repeated 0 or more times". The next . is escaped because it's a special character, and you want a literal period (".php"). Don't forget that if you're typing this in as a literal string in something like C#, Java, etc., you need to escape the backslash because it's a special character in many literal strings.






              share|improve this answer
























              • This should also work: ^wp.+.php$ The only difference is the '+', which makes the dot "greedy".

                – Adam Howell
                Apr 8 '16 at 3:35








              • 4





                @AdamHowell + and * are both greedy, although greediness doesn't make a difference in this case. The difference is that * matches zero or more characters, and the + matches one or more characters. So if someone wanted to match "wp.php", they should use *; if they specifically wanted to not match that, they should use +.

                – Michelle
                Apr 11 '16 at 10:34














              33












              33








              33







              ^wp.*.php$ Should do the trick.



              The .* means "any character, repeated 0 or more times". The next . is escaped because it's a special character, and you want a literal period (".php"). Don't forget that if you're typing this in as a literal string in something like C#, Java, etc., you need to escape the backslash because it's a special character in many literal strings.






              share|improve this answer













              ^wp.*.php$ Should do the trick.



              The .* means "any character, repeated 0 or more times". The next . is escaped because it's a special character, and you want a literal period (".php"). Don't forget that if you're typing this in as a literal string in something like C#, Java, etc., you need to escape the backslash because it's a special character in many literal strings.







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Aug 2 '13 at 19:05









              MichelleMichelle

              2,3442030




              2,3442030













              • This should also work: ^wp.+.php$ The only difference is the '+', which makes the dot "greedy".

                – Adam Howell
                Apr 8 '16 at 3:35








              • 4





                @AdamHowell + and * are both greedy, although greediness doesn't make a difference in this case. The difference is that * matches zero or more characters, and the + matches one or more characters. So if someone wanted to match "wp.php", they should use *; if they specifically wanted to not match that, they should use +.

                – Michelle
                Apr 11 '16 at 10:34



















              • This should also work: ^wp.+.php$ The only difference is the '+', which makes the dot "greedy".

                – Adam Howell
                Apr 8 '16 at 3:35








              • 4





                @AdamHowell + and * are both greedy, although greediness doesn't make a difference in this case. The difference is that * matches zero or more characters, and the + matches one or more characters. So if someone wanted to match "wp.php", they should use *; if they specifically wanted to not match that, they should use +.

                – Michelle
                Apr 11 '16 at 10:34

















              This should also work: ^wp.+.php$ The only difference is the '+', which makes the dot "greedy".

              – Adam Howell
              Apr 8 '16 at 3:35







              This should also work: ^wp.+.php$ The only difference is the '+', which makes the dot "greedy".

              – Adam Howell
              Apr 8 '16 at 3:35






              4




              4





              @AdamHowell + and * are both greedy, although greediness doesn't make a difference in this case. The difference is that * matches zero or more characters, and the + matches one or more characters. So if someone wanted to match "wp.php", they should use *; if they specifically wanted to not match that, they should use +.

              – Michelle
              Apr 11 '16 at 10:34





              @AdamHowell + and * are both greedy, although greediness doesn't make a difference in this case. The difference is that * matches zero or more characters, and the + matches one or more characters. So if someone wanted to match "wp.php", they should use *; if they specifically wanted to not match that, they should use +.

              – Michelle
              Apr 11 '16 at 10:34











              4














              Example:
              ajshdjashdjashdlasdlhdlSTARTasdasdsdaasdENDaknsdklansdlknaldknaaklsdn



              1) STARTw*END
              return: STARTasdasdsdaasdEND - will give you words between START and END



              2) STARTd*END
              return: START12121212END - will give you numbers between START and END



              3) STARTd*_d*END
              return: START1212_1212END - will give you numbers between START and END having _






              share|improve this answer




























                4














                Example:
                ajshdjashdjashdlasdlhdlSTARTasdasdsdaasdENDaknsdklansdlknaldknaaklsdn



                1) STARTw*END
                return: STARTasdasdsdaasdEND - will give you words between START and END



                2) STARTd*END
                return: START12121212END - will give you numbers between START and END



                3) STARTd*_d*END
                return: START1212_1212END - will give you numbers between START and END having _






                share|improve this answer


























                  4












                  4








                  4







                  Example:
                  ajshdjashdjashdlasdlhdlSTARTasdasdsdaasdENDaknsdklansdlknaldknaaklsdn



                  1) STARTw*END
                  return: STARTasdasdsdaasdEND - will give you words between START and END



                  2) STARTd*END
                  return: START12121212END - will give you numbers between START and END



                  3) STARTd*_d*END
                  return: START1212_1212END - will give you numbers between START and END having _






                  share|improve this answer













                  Example:
                  ajshdjashdjashdlasdlhdlSTARTasdasdsdaasdENDaknsdklansdlknaldknaaklsdn



                  1) STARTw*END
                  return: STARTasdasdsdaasdEND - will give you words between START and END



                  2) STARTd*END
                  return: START12121212END - will give you numbers between START and END



                  3) STARTd*_d*END
                  return: START1212_1212END - will give you numbers between START and END having _







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Dec 31 '16 at 11:37









                  Nayan HodarNayan Hodar

                  1896




                  1896






























                      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%2f18024298%2fregular-expression-starting-and-ending-with-a-character-string%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







                      Popular posts from this blog

                      Xamarin.iOS Cant Deploy on Iphone

                      Glorious Revolution

                      Dulmage-Mendelsohn matrix decomposition in Python