473,795 Members | 3,441 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

problem with regular expressions following code snippet...

I'm definately not a regexp wiz.... need some suggestions here:

grab PHP_SELF, strip the end filename off and the 2 directories
preceeding it...

This is what I've come up with so far, except I'd like to replace the
..html for any extension and I've tried it but doesn't work... and I've
also tried parsing backing a second directory but no luck, maybe you
have an alternative solution:

--- snip ---

What I have tried:

// retrieves URL of itself, then strips the directories only leaving
the filename
$PHP_SELF = $_SERVER['PHP_SELF'];
$new = preg_replace("/[^\/](.*?)[$\/]/mis",'',$PHP_SE LF);
$new = preg_replace("/\//mis",'',$new);

// replace the file only so we have the directory and append the
contact form onto it
$expression =
sprintf("/[$\/](?=...)([a-z0-9]+.\.[a-z]+)/mi",$new); // or

$expression = sprintf("/[$\/](?=...)([a-z0-9]+.\.htm?)/mi",$new); or

$expression =
sprintf("/[$\/](?=...)([a-z0-9]+.\.htm?)(?<=.. .)(-[a-z][\/])/mi",$new);
$contactform = preg_replace($e xpression,'',$P HP_SELF);
$contactform = $contactform.'/contact_form1.h tml';
What I have now:

// retrieves URL of itself, then strips the directories only leaving
the filename
$PHP_SELF = $_SERVER['PHP_SELF'];
$new = preg_replace("/[^\/](.*?)[$\/]/mis",'',$PHP_SE LF);
$new = preg_replace("/\//mis",'',$new);

// replace the file only so we have the directory and append the
contact form onto it
$expression = sprintf("/[$\/](?=...)%s/mi",$new);
$contactform = preg_replace($e xpression,'',$P HP_SELF);
$contactform = $contactform.'/contact_form1.h tml';

--- snip ---

PHP_SELF = /dirname1/dirname2/dirname3/dirname4/somefile.html

which generally results in
/dirname1/dirname2/dirname3/contact_form1.h tml

but what I would like is to also remove dirname3 as well resulting in:

/dirname1/dirname2/contact_form1.h tml

Jul 17 '05 #1
2 1643
Anyone out there know regex???

Jul 17 '05 #2
Ok, after a little more searching, a little more clearer thinking I
sovled it with:

/[\/]\w+[\/](\w+\.[a-z]{3,4})/mi

Jul 17 '05 #3

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

Similar topics

0
1405
by: | last post by:
I'm trying to scan a (binary) file for a string matching a particular pattern, and am getting unexpected results. I don't know if this is a bug or just my own misunderstanding of regular expressions. The string I'm searching for is a "versioned file name" of the form: "AMS_epXXXx.flt", where 'XXX' is 1 to 3 numerals, the 'x' is lower case 'a-z', and the '_' and 'ep' are each optional. In other words, the following are examples that...
1
4185
by: Kenneth McDonald | last post by:
I'm working on the 0.8 release of my 'rex' module, and would appreciate feedback, suggestions, and criticism as I work towards finalizing the API and feature sets. rex is a module intended to make regular expressions easier to create and use (and in my experience as a regular expression user, it makes them MUCH easier to create and use.) I'm still working on formal documentation, and in any case, such documentation isn't necessarily the...
4
305
by: Steve | last post by:
Hi all I have to validate a password to determine whether or not it adheres to certain rules. For example, the password must contain at least 1 number, at least 1 uppercase character and at least 1 lowercase character. Are regular expressions the way to perform this validation? I know I can hard-code it in at the moment, but the rules may change in the future and I'd like to store the expression in an external file so it can be altered...
4
1760
by: Arne | last post by:
Hi, I'm trying to recognise phone numbers on the following format using regular expressions: +47 123 45 678 This should be recognised by the following regular expression: +47 \d{3} \d{2} \d{3} but it's not. This regular expression: +47 \d{2} \d{2} \d{2} \d{2} DOES recognise the following phone number: +47 12 34 56 78
4
2184
by: Ben Dewey | last post by:
Hey, I have only been playing with regular expressions for some time. I am working on some code that parses and object 560 event log. I have created two expressions the first one which works okay is for the actual csv of each log. The second one parses out the description of the log. My problem is with the accesses section of the description. How do I parse multiple groups that have the same name. When I do a for each through the...
1
2055
by: Daniel Walzenbach | last post by:
Hi, does anybody know I can extract a substring of a text with regular expressions. Let’s consider the following text: “Regular expressions are often used to make sure that a string matches a certain pattern.”. I e.g. want to extract everything between “expressions” and “a” which is in the above example “are often used to make sure that”. The unfortunately best I could do was the following piece of code: Dim...
5
1659
by: Greg Vereschagin | last post by:
I'm trying to figure out a regular expression that will match the innermost tag and the contents in between. Specifically, the string that I am attempting to match looks as follows: ....<table>...<table>...>Final<...</table>...</table>... I want to match: <table>...>Final<...</table> from this example. The string could also, of course, look like the following:
5
1595
by: Kijak | last post by:
Hi, Im just starting working with REGEX and got a few problems. Could you tell me how to test if two strings can be found in another. ei: String to test: "This is a great car you got" Look for: "great" AND "car" and what about upper case
1
9152
by: Engineerik | last post by:
According to what I see in the manuals I should be able to use regular expressions with the rowfilter property of a dataview but I keep getting errors. Example: I want to find any rowview where column APCode is not a string of exactly 3 uppercase letters. Assume dtBAirpt is an instance of a datatable having a column APCode Code snippet Try Dim dvTest As New DataView(dtBAirpt, _
13
7493
by: Wiseman | last post by:
I'm kind of disappointed with the re regular expressions module. In particular, the lack of support for recursion ( (?R) or (?n) ) is a major drawback to me. There are so many great things that can be accomplished with regular expressions this way, such as validating a mathematical expression or parsing a language with nested parens, quoting or expressions. Another feature I'm missing is once-only subpatterns and possessive quantifiers...
0
9672
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, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
9519
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10164
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
6780
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5437
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4113
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
3723
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2920
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.