473,657 Members | 2,392 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

RegEx and exclusion

Hello,

I'm pretty new to PHP and am wrestling with regexp's right now. I am
trying to figure something out with no luck. Hopefully someone can
help.

I am trying to filter out file names. I want to write a regexp that
matches all image file names, such as ".jpg", ".bmp", ".png", etc...
but not file names that end with "_thumb.*** " (where *** is the
extension of the file name... which can be 3 or 4 chars long). So...

somepic.jpg <--- should match
somepic_thumb.j pg <--- should not match
anotherpic.png <--- should match
this_thumb.is_b roken.jpg <--- should match

Currently, I do this using two regexps. First, I compare to determine
if the file is an image by looking at the end and seeing if it's .jpg,
..png, etc. If that passes, I compare again to see if it contains
"_thumb.*** " at the end (allowing for 3 or 4 chars, such as jpg or
jpeg), as such:

if(preg_match("/(.jpg|.jpeg|.gi f|.png|.bmp)$/i", $filename)){
if(!preg_match( "/(_thumb.).{3,4} $/i", $filename)){
$files[]=$filename;
print("$filenam e<br>");
}
}

How can I, if possible, combine these into one regexp?

Thanks for your time,
Mr Phuzz

Oct 1 '06 #1
2 1913
Mr Phuzz wrote:
Hello,

I'm pretty new to PHP and am wrestling with regexp's right now. I am
trying to figure something out with no luck. Hopefully someone can
help.

I am trying to filter out file names. I want to write a regexp that
matches all image file names, such as ".jpg", ".bmp", ".png", etc...
but not file names that end with "_thumb.*** " (where *** is the
extension of the file name... which can be 3 or 4 chars long). So...

somepic.jpg <--- should match
somepic_thumb.j pg <--- should not match
anotherpic.png <--- should match
this_thumb.is_b roken.jpg <--- should match

Currently, I do this using two regexps. First, I compare to determine
if the file is an image by looking at the end and seeing if it's .jpg,
.png, etc. If that passes, I compare again to see if it contains
"_thumb.*** " at the end (allowing for 3 or 4 chars, such as jpg or
jpeg), as such:

if(preg_match("/(.jpg|.jpeg|.gi f|.png|.bmp)$/i", $filename)){
if(!preg_match( "/(_thumb.).{3,4} $/i", $filename)){
$files[]=$filename;
print("$filenam e<br>");
}
}

How can I, if possible, combine these into one regexp?

Thanks for your time,
Mr Phuzz
You can do it by a negative lookbehind assertion:

'/(?<!_thumb)\.(j pg|jpeg|gif|png |bmp)$/'

finds an instance of .jpg etc that is not preceded by _thumb

(You want to escape the . anyway, as otherwise it will match any character.)

Colin
Oct 1 '06 #2
Colin Fine wrote:
Mr Phuzz wrote:
>Hello,

I'm pretty new to PHP and am wrestling with regexp's right now. I am
trying to figure something out with no luck. Hopefully someone can
help.

I am trying to filter out file names. I want to write a regexp that
matches all image file names, such as ".jpg", ".bmp", ".png", etc...
but not file names that end with "_thumb.*** " (where *** is the
extension of the file name... which can be 3 or 4 chars long). So...

somepic.jpg <--- should match
somepic_thumb. jpg <--- should not match
anotherpic.p ng <--- should match
this_thumb.is_ broken.jpg <--- should match

Currently, I do this using two regexps. First, I compare to determine
if the file is an image by looking at the end and seeing if it's .jpg,
.png, etc. If that passes, I compare again to see if it contains
"_thumb.*** " at the end (allowing for 3 or 4 chars, such as jpg or
jpeg), as such:

if(preg_match("/(.jpg|.jpeg|.gi f|.png|.bmp)$/i", $filename)){
if(!preg_match( "/(_thumb.).{3,4} $/i", $filename)){
$files[]=$filename;
print("$filenam e<br>");
}
}

How can I, if possible, combine these into one regexp?

Thanks for your time,
Mr Phuzz

You can do it by a negative lookbehind assertion:

'/(?<!_thumb)\.(j pg|jpeg|gif|png |bmp)$/'

finds an instance of .jpg etc that is not preceded by _thumb

(You want to escape the . anyway, as otherwise it will match any
character.)

Colin
But on second thought, many people would prefer two matches (even if it
is a tiny bit less efficient) rather than using such an obscure pattern.

You could then also capture the file type and use it in the second pattern:

if(preg_match("/\.(jpg|jpeg|gif |png|bmp)$/i", $filename, $match) &&
!preg_match("/_thumb\.$match[1]$/i", $filename)){
Colin
Oct 2 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
3442
by: Dean H. Saxe | last post by:
I'm currently developing a tool in perl to search out potential XSS (Cross Site Scripting) vulnerabilities and correct them in a ColdFusion based web app. I've been having great success so far, however, one scenario has me banging my head against the wall. I need a regex to find all <cfoutput ...>...</cfoutput> blocks in a CFM template. The regex should find all such blocks that are *not* nested within HTML tags (the tag itself,...
5
2220
by: clusardi2k | last post by:
Hello, I have a assignment just thrown onto my desk. What is the easiest way to solve it? Below is a brief description of the task. There are multible programs which use the same library routine which is an interface to what I'll call a service program.
1
1740
by: howard dierking | last post by:
Hi all, I'm having a problem with a reg ex. Essentiall, I'm trying to isolate variable declarations from old vbscript where there was no explicit declaration requirement. This should seem easy enough - just identify by the assignment statement; except for the fact that vb uses a single '=' token for comparison as well as assignment. Therefore, I need a regex that will match assignment statements while excluding if statements. I...
78
4582
by: wkehowski | last post by:
The python code below generates a cartesian product subject to any logical combination of wildcard exclusions. For example, suppose I want to generate a cartesian product S^n, n>=3, of that excludes '*a*b*' and '*c*d*a*'. See below for details. CHALLENGE: generate an equivalent in ruby, lisp, haskell, ocaml, or in a CAS like maple or mathematica. #------------------------------------------------------------------------------- # Short...
6
2494
by: Extremest | last post by:
I have a huge regex setup going on. If I don't do each one by itself instead of all in one it won't work for. Also would like to know if there is a faster way tried to use string.replace with all the right parts in there in one big line and for some reason that did not work either. Here is my regex's. static Regex rar = new Regex("\\.part.*", RegexOptions.IgnoreCase); static Regex par = new Regex("\\.vol.*", RegexOptions.IgnoreCase);
5
2290
by: TheSteph | last post by:
Hi, I'm new to Regex.. Could someone show me how I can extract substring enclosed in ? Example :
1
1883
by: illegal.prime | last post by:
So I have a container of objects that I don't want to iterate across when I'm modifying it. I.E. I lock on adds and deletes to the container - so that my traversals of it don't result in concurrency issues. However, what do I need to do to allow multiple threads to traverse the container without synchronization/mutual-exclusion - but ensure that synchronization/mutual-exclusion is there when the container is trying to be both changed...
15
50217
by: morleyc | last post by:
Hi, i would like to remove a number of characters from my string (\t \r \n which are throughout the string), i know regex can do this but i have no idea how. Any pointers much appreciated. Chris
6
4008
by: Scott | last post by:
I have two threads that share a python list. One thread adds to the list with append(), the other thread removes items with pop(). My question is -- are python list operations atomic? If they are not, then I assume I need to put some mutual exclusion around the append() and pop() calls ? Thanks, Scott
5
2052
by: Patrick A | last post by:
All, I'm dealing with a 3rd party application that can't interpret the Access function FormatCurrency. When it encounters my query with "Exclu: FormatCurrency(, 0)", it arfs. The vendor says I have to use "straight SQL." Can someone point me to a good SQL reference where I can find the SQL eqiuvalent, assuming there is one?
0
8411
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8838
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8739
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8513
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8613
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7351
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4329
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2740
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1732
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.