regex match all not PDF files [duplicate]





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







1
















This question already has an answer here:




  • Regex pattern that does not match certain extensions?

    5 answers



  • Regular expression for excluding file types .exe and .js

    3 answers




preg_match('/.(?!pdf)$/', $file)



I want to match all files except pdf files



path/file.jpg # match
path/file.png # match
path/file.pdf # not match









share|improve this question















marked as duplicate by revo 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 3 at 12:06


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.



















  • if the file has a name like path/pdf_file.png

    – executable
    Jan 3 at 10:46











  • match all files except pdf files

    – clarkk
    Jan 3 at 10:47













  • .(?!pdf$).+$

    – splash58
    Jan 3 at 11:05











  • @splash58 Please avoid posting answers as comments; it makes it harder to find, and impossible to edit, vote on, etc

    – IMSoP
    Jan 3 at 11:06











  • You should not use all the recommended complicated regular expressions below. Use $(?<!.pdf) instead.

    – revo
    Jan 3 at 12:08


















1
















This question already has an answer here:




  • Regex pattern that does not match certain extensions?

    5 answers



  • Regular expression for excluding file types .exe and .js

    3 answers




preg_match('/.(?!pdf)$/', $file)



I want to match all files except pdf files



path/file.jpg # match
path/file.png # match
path/file.pdf # not match









share|improve this question















marked as duplicate by revo 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 3 at 12:06


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.



















  • if the file has a name like path/pdf_file.png

    – executable
    Jan 3 at 10:46











  • match all files except pdf files

    – clarkk
    Jan 3 at 10:47













  • .(?!pdf$).+$

    – splash58
    Jan 3 at 11:05











  • @splash58 Please avoid posting answers as comments; it makes it harder to find, and impossible to edit, vote on, etc

    – IMSoP
    Jan 3 at 11:06











  • You should not use all the recommended complicated regular expressions below. Use $(?<!.pdf) instead.

    – revo
    Jan 3 at 12:08














1












1








1


0







This question already has an answer here:




  • Regex pattern that does not match certain extensions?

    5 answers



  • Regular expression for excluding file types .exe and .js

    3 answers




preg_match('/.(?!pdf)$/', $file)



I want to match all files except pdf files



path/file.jpg # match
path/file.png # match
path/file.pdf # not match









share|improve this question

















This question already has an answer here:




  • Regex pattern that does not match certain extensions?

    5 answers



  • Regular expression for excluding file types .exe and .js

    3 answers




preg_match('/.(?!pdf)$/', $file)



I want to match all files except pdf files



path/file.jpg # match
path/file.png # match
path/file.pdf # not match




This question already has an answer here:




  • Regex pattern that does not match certain extensions?

    5 answers



  • Regular expression for excluding file types .exe and .js

    3 answers








php regex






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 3 at 11:15









splash58

21.9k31225




21.9k31225










asked Jan 3 at 10:45









clarkkclarkk

8,01248145239




8,01248145239




marked as duplicate by revo 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 3 at 12:06


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 revo 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 3 at 12:06


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.















  • if the file has a name like path/pdf_file.png

    – executable
    Jan 3 at 10:46











  • match all files except pdf files

    – clarkk
    Jan 3 at 10:47













  • .(?!pdf$).+$

    – splash58
    Jan 3 at 11:05











  • @splash58 Please avoid posting answers as comments; it makes it harder to find, and impossible to edit, vote on, etc

    – IMSoP
    Jan 3 at 11:06











  • You should not use all the recommended complicated regular expressions below. Use $(?<!.pdf) instead.

    – revo
    Jan 3 at 12:08



















  • if the file has a name like path/pdf_file.png

    – executable
    Jan 3 at 10:46











  • match all files except pdf files

    – clarkk
    Jan 3 at 10:47













  • .(?!pdf$).+$

    – splash58
    Jan 3 at 11:05











  • @splash58 Please avoid posting answers as comments; it makes it harder to find, and impossible to edit, vote on, etc

    – IMSoP
    Jan 3 at 11:06











  • You should not use all the recommended complicated regular expressions below. Use $(?<!.pdf) instead.

    – revo
    Jan 3 at 12:08

















if the file has a name like path/pdf_file.png

– executable
Jan 3 at 10:46





if the file has a name like path/pdf_file.png

– executable
Jan 3 at 10:46













match all files except pdf files

– clarkk
Jan 3 at 10:47







match all files except pdf files

– clarkk
Jan 3 at 10:47















.(?!pdf$).+$

– splash58
Jan 3 at 11:05





.(?!pdf$).+$

– splash58
Jan 3 at 11:05













@splash58 Please avoid posting answers as comments; it makes it harder to find, and impossible to edit, vote on, etc

– IMSoP
Jan 3 at 11:06





@splash58 Please avoid posting answers as comments; it makes it harder to find, and impossible to edit, vote on, etc

– IMSoP
Jan 3 at 11:06













You should not use all the recommended complicated regular expressions below. Use $(?<!.pdf) instead.

– revo
Jan 3 at 12:08





You should not use all the recommended complicated regular expressions below. Use $(?<!.pdf) instead.

– revo
Jan 3 at 12:08












3 Answers
3






active

oldest

votes


















1














Instead of using regex, you can take the extension of your file and check if it's a pdf.



$ext = pathinfo($file, PATHINFO_EXTENSION);
if($ext != 'pdf'){
echo "I'm not a pdf";
}


If you prefer using regex



<?php
$file = array("path/file.jpg", "path/file.png", "path/file.pdf");
foreach ($file as &$value) {
preg_match('/^(.(?!.*.pdf$))*$/', $value, $matches);
if(!empty($matches)){
echo " $value is not a pdf";
}
}


https://regex101.com/r/7C7YsR/1






share|improve this answer


























  • I prefer regex..

    – clarkk
    Jan 3 at 10:53



















1














If you want to use a regex, this one will work:



^(?!.*.pdf).*$


It simply uses a negative lookahead to assert that the filename doesn't end in .pdf and then .* to match everything in the filename.



Demo on regex101



In PHP:



$filenames = array('path/file.jpg','path/file.png','path/file.pdf');
foreach ($filenames as $filename) {
if (preg_match('/^(?!.*.pdf).*$/', $filename)) echo ""$filename" is not a PDF file.n";
}


Output:



"path/file.jpg" is not a PDF file. 
"path/file.png" is not a PDF file.


Demo on 3v4l.org






share|improve this answer































    0














    Asserting that something is not present in a regex is often fiddly, because a regex works by looking through the input for things to match.



    The approach you've used is a "zero-width negative look-ahead assertion" - it matches anywhere in the string that isn't followed by pdf, but doesn't "use up" any input. So your regex doesn't work because the rest of it still needs to match, and that is /.$/, meaning ". at end of string".



    The simplest approach is to look for strings that do end .pdf, e.g. if ( ! preg_match('/.pdf$/', $file) ) { ... }.






    share|improve this answer






























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      Instead of using regex, you can take the extension of your file and check if it's a pdf.



      $ext = pathinfo($file, PATHINFO_EXTENSION);
      if($ext != 'pdf'){
      echo "I'm not a pdf";
      }


      If you prefer using regex



      <?php
      $file = array("path/file.jpg", "path/file.png", "path/file.pdf");
      foreach ($file as &$value) {
      preg_match('/^(.(?!.*.pdf$))*$/', $value, $matches);
      if(!empty($matches)){
      echo " $value is not a pdf";
      }
      }


      https://regex101.com/r/7C7YsR/1






      share|improve this answer


























      • I prefer regex..

        – clarkk
        Jan 3 at 10:53
















      1














      Instead of using regex, you can take the extension of your file and check if it's a pdf.



      $ext = pathinfo($file, PATHINFO_EXTENSION);
      if($ext != 'pdf'){
      echo "I'm not a pdf";
      }


      If you prefer using regex



      <?php
      $file = array("path/file.jpg", "path/file.png", "path/file.pdf");
      foreach ($file as &$value) {
      preg_match('/^(.(?!.*.pdf$))*$/', $value, $matches);
      if(!empty($matches)){
      echo " $value is not a pdf";
      }
      }


      https://regex101.com/r/7C7YsR/1






      share|improve this answer


























      • I prefer regex..

        – clarkk
        Jan 3 at 10:53














      1












      1








      1







      Instead of using regex, you can take the extension of your file and check if it's a pdf.



      $ext = pathinfo($file, PATHINFO_EXTENSION);
      if($ext != 'pdf'){
      echo "I'm not a pdf";
      }


      If you prefer using regex



      <?php
      $file = array("path/file.jpg", "path/file.png", "path/file.pdf");
      foreach ($file as &$value) {
      preg_match('/^(.(?!.*.pdf$))*$/', $value, $matches);
      if(!empty($matches)){
      echo " $value is not a pdf";
      }
      }


      https://regex101.com/r/7C7YsR/1






      share|improve this answer















      Instead of using regex, you can take the extension of your file and check if it's a pdf.



      $ext = pathinfo($file, PATHINFO_EXTENSION);
      if($ext != 'pdf'){
      echo "I'm not a pdf";
      }


      If you prefer using regex



      <?php
      $file = array("path/file.jpg", "path/file.png", "path/file.pdf");
      foreach ($file as &$value) {
      preg_match('/^(.(?!.*.pdf$))*$/', $value, $matches);
      if(!empty($matches)){
      echo " $value is not a pdf";
      }
      }


      https://regex101.com/r/7C7YsR/1







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Jan 3 at 11:13

























      answered Jan 3 at 10:49









      executableexecutable

      1,9832924




      1,9832924













      • I prefer regex..

        – clarkk
        Jan 3 at 10:53



















      • I prefer regex..

        – clarkk
        Jan 3 at 10:53

















      I prefer regex..

      – clarkk
      Jan 3 at 10:53





      I prefer regex..

      – clarkk
      Jan 3 at 10:53













      1














      If you want to use a regex, this one will work:



      ^(?!.*.pdf).*$


      It simply uses a negative lookahead to assert that the filename doesn't end in .pdf and then .* to match everything in the filename.



      Demo on regex101



      In PHP:



      $filenames = array('path/file.jpg','path/file.png','path/file.pdf');
      foreach ($filenames as $filename) {
      if (preg_match('/^(?!.*.pdf).*$/', $filename)) echo ""$filename" is not a PDF file.n";
      }


      Output:



      "path/file.jpg" is not a PDF file. 
      "path/file.png" is not a PDF file.


      Demo on 3v4l.org






      share|improve this answer




























        1














        If you want to use a regex, this one will work:



        ^(?!.*.pdf).*$


        It simply uses a negative lookahead to assert that the filename doesn't end in .pdf and then .* to match everything in the filename.



        Demo on regex101



        In PHP:



        $filenames = array('path/file.jpg','path/file.png','path/file.pdf');
        foreach ($filenames as $filename) {
        if (preg_match('/^(?!.*.pdf).*$/', $filename)) echo ""$filename" is not a PDF file.n";
        }


        Output:



        "path/file.jpg" is not a PDF file. 
        "path/file.png" is not a PDF file.


        Demo on 3v4l.org






        share|improve this answer


























          1












          1








          1







          If you want to use a regex, this one will work:



          ^(?!.*.pdf).*$


          It simply uses a negative lookahead to assert that the filename doesn't end in .pdf and then .* to match everything in the filename.



          Demo on regex101



          In PHP:



          $filenames = array('path/file.jpg','path/file.png','path/file.pdf');
          foreach ($filenames as $filename) {
          if (preg_match('/^(?!.*.pdf).*$/', $filename)) echo ""$filename" is not a PDF file.n";
          }


          Output:



          "path/file.jpg" is not a PDF file. 
          "path/file.png" is not a PDF file.


          Demo on 3v4l.org






          share|improve this answer













          If you want to use a regex, this one will work:



          ^(?!.*.pdf).*$


          It simply uses a negative lookahead to assert that the filename doesn't end in .pdf and then .* to match everything in the filename.



          Demo on regex101



          In PHP:



          $filenames = array('path/file.jpg','path/file.png','path/file.pdf');
          foreach ($filenames as $filename) {
          if (preg_match('/^(?!.*.pdf).*$/', $filename)) echo ""$filename" is not a PDF file.n";
          }


          Output:



          "path/file.jpg" is not a PDF file. 
          "path/file.png" is not a PDF file.


          Demo on 3v4l.org







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 3 at 11:09









          NickNick

          39.4k132443




          39.4k132443























              0














              Asserting that something is not present in a regex is often fiddly, because a regex works by looking through the input for things to match.



              The approach you've used is a "zero-width negative look-ahead assertion" - it matches anywhere in the string that isn't followed by pdf, but doesn't "use up" any input. So your regex doesn't work because the rest of it still needs to match, and that is /.$/, meaning ". at end of string".



              The simplest approach is to look for strings that do end .pdf, e.g. if ( ! preg_match('/.pdf$/', $file) ) { ... }.






              share|improve this answer




























                0














                Asserting that something is not present in a regex is often fiddly, because a regex works by looking through the input for things to match.



                The approach you've used is a "zero-width negative look-ahead assertion" - it matches anywhere in the string that isn't followed by pdf, but doesn't "use up" any input. So your regex doesn't work because the rest of it still needs to match, and that is /.$/, meaning ". at end of string".



                The simplest approach is to look for strings that do end .pdf, e.g. if ( ! preg_match('/.pdf$/', $file) ) { ... }.






                share|improve this answer


























                  0












                  0








                  0







                  Asserting that something is not present in a regex is often fiddly, because a regex works by looking through the input for things to match.



                  The approach you've used is a "zero-width negative look-ahead assertion" - it matches anywhere in the string that isn't followed by pdf, but doesn't "use up" any input. So your regex doesn't work because the rest of it still needs to match, and that is /.$/, meaning ". at end of string".



                  The simplest approach is to look for strings that do end .pdf, e.g. if ( ! preg_match('/.pdf$/', $file) ) { ... }.






                  share|improve this answer













                  Asserting that something is not present in a regex is often fiddly, because a regex works by looking through the input for things to match.



                  The approach you've used is a "zero-width negative look-ahead assertion" - it matches anywhere in the string that isn't followed by pdf, but doesn't "use up" any input. So your regex doesn't work because the rest of it still needs to match, and that is /.$/, meaning ". at end of string".



                  The simplest approach is to look for strings that do end .pdf, e.g. if ( ! preg_match('/.pdf$/', $file) ) { ... }.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jan 3 at 11:11









                  IMSoPIMSoP

                  48.3k65895




                  48.3k65895















                      Popular posts from this blog

                      MongoDB - Not Authorized To Execute Command

                      How to fix TextFormField cause rebuild widget in Flutter

                      in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith