1
votes

I am trying to write a regular expression to validate file names on a file system.

Examples of valid file names are

  • tapa_newcougar_org.png
  • tapa_lamborghini-talk_com.png
  • tapa_clubfrontier_org.png

The logic behind valid is the image starts with tapa followed by an underscore. Then the domain name followed by _ the tld (com, org, net)

Thanks

3

3 Answers

5
votes
preg_match('/^tapa_[a-z0-9-_]+?_(com|org|net)\.png$/', $string);

That ought to do it (tested). If you want to match capital letters too, add the i (case insensitive) flag like this:

preg_match('/^tapa_[a-z0-9-_]+?_(com|org|net)\.png$/i', $string);

For a graphical representation of how this works, you can paste it in here: http://strfriend.com

1
votes

You could try:

preg_match('/^tapa_.+_(com|org|net)\.png$/', $filename);
0
votes

^tapa_[a-zA-Z0-9\-_]+_(com|org|net)\.png$ should work for you.