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.png <--- 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|.gif|.png|.bmp)$/i", $filename)){
if(!preg_match("/(_thumb.).{3,4}$/i", $filename)){
$files[]=$filename;
print("$filename<br>");
}
}
How can I, if possible, combine these into one regexp?
Thanks for your time,
Mr Phuzz 2 1901
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.png <--- 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|.gif|.png|.bmp)$/i", $filename)){
if(!preg_match("/(_thumb.).{3,4}$/i", $filename)){
$files[]=$filename;
print("$filename<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)\.(jpg|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
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.png <--- 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|.gif|.png|.bmp)$/i", $filename)){ if(!preg_match("/(_thumb.).{3,4}$/i", $filename)){ $files[]=$filename; print("$filename<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)\.(jpg|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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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,...
|
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...
|
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...
|
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...
|
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...
| |
by: TheSteph |
last post by:
Hi,
I'm new to Regex..
Could someone show me how I can extract substring enclosed in ?
Example :
|
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...
|
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
|
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...
|
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...
|
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,...
| |
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...
|
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...
|
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...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
| |
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 ...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |