473,775 Members | 2,625 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Hard time with a regex...

Hello,

I'm trying to extract the home page URL out of a any URL from the same
web site
For instance if I'm on http://www.regular-expressions.info/...ptexample.html
I want to extract http://www.regular-expressions.info
So, I have my regex ready, it's working fine: http://[\w.-]+
But when I add it to PHP's preg_match function this way:

preg_match('/http\:\/\/\[\\w.-\]+/','http://www.regular-
expressions.inf o/javascriptexamp le.html',$match es);

I get this error message:

Warning: preg_match() [function.preg-match]: Delimiter must not be
alphanumeric or backslash
I'm a little lost, I read this page http://www.php.net/manual/en/function.preg-match.php
and I wasn't able to find what is wrong. On the page they put slashes
around the regex, sometimes they don't... Do you know what is causing
the error message?
Thanks,

--
Charles.
Nov 30 '07 #1
6 1680
On Nov 29, 8:39 pm, Charles <landema...@gma il.comwrote:
Hello,

I'm trying to extract the home page URL out of a any URL from the same
web site
For instance if I'm onhttp://www.regular-expressions.inf o/javascriptexamp le.html
I want to extracthttp://www.regular-expressions.inf o
So, I have my regex ready, it's working fine:http://[\w.-]+
But when I add it to PHP's preg_match function this way:

preg_match('/http\:\/\/\[\\w.-\]+/','http://www.regular-
expressions.inf o/javascriptexamp le.html',$match es);

I get this error message:

Warning: preg_match() [function.preg-match]: Delimiter must not be
alphanumeric or backslash
I'm a little lost, I read this pagehttp://www.php.net/manual/en/function.preg-match.php
and I wasn't able to find what is wrong. On the page they put slashes
around the regex, sometimes they don't... Do you know what is causing
the error message?
Thanks,

--
Charles.
Why not make life easy and use this instead:

<http://www.php.net/parse-url>
Nov 30 '07 #2
Charles wrote:
Hello,

I'm trying to extract the home page URL out of a any URL from the same
web site
For instance if I'm on http://www.regular-expressions.info/...ptexample.html
I want to extract http://www.regular-expressions.info
So, I have my regex ready, it's working fine: http://[\w.-]+
But when I add it to PHP's preg_match function this way:

preg_match('/http\:\/\/\[\\w.-\]+/','http://www.regular-
expressions.inf o/javascriptexamp le.html',$match es);

I get this error message:

Warning: preg_match() [function.preg-match]: Delimiter must not be
alphanumeric or backslash
I'm a little lost, I read this page http://www.php.net/manual/en/function.preg-match.php
and I wasn't able to find what is wrong. On the page they put slashes
around the regex, sometimes they don't... Do you know what is causing
the error message?
Thanks,

--
Charles.
Or just use this if you are running the script on the server whose name
you are trying to find: $_SERVER["SERVER_NAM E"]
Nov 30 '07 #3
Charles wrote:
preg_match('/http\:\/\/\[\\w.-\]+/','http://www.regular-
expressions.inf o/javascriptexamp le.html',$match es);
Firstly, don't backlash-escape the square brackets:

preg_match('/http\:\/\/[\\w.-]+/','http://www.regular-expressions.inf o/
javascriptexamp le.html',$match es);

That should work. But there's still room for improvement -- to make it
more readable. You don't need to backslash escape the backslash itself,
nor the colon:

preg_match('/http:\/\/[\w.-]+/','http://www.regular-expressions.inf o/
javascriptexamp le.html',$match es);

Also, PHP (and Perl) allows you to choose a character other than slash as
a delimited (i.e. the character at the beginning and end of the
expression). In this case, lets choose a hash instead:

preg_match('#ht tp:\/\/[\w.-]+#','http://www.regular-expressions.inf o/
javascriptexamp le.html',$match es);

Because we're not using a slash as a delimiter, it means we no longer need
to backslash-escape the slashes within the expression:

preg_match('#ht tp://[\w.-]+#','http://www.regular-expressions.inf o/
javascriptexamp le.html',$match es);

That's much more readable, right?

--
Toby A Inkster BSc (Hons) ARCS
[Geek of HTML/SQL/Perl/PHP/Python/Apache/Linux]
[OS: Linux 2.6.17.14-mm-desktop-9mdvsmp, up 5 days, 17:19.]

Sharing Music with Apple iTunes
http://tobyinkster.co.uk/blog/2007/1...tunes-sharing/
Nov 30 '07 #4
Charles:
I'm trying to extract the home page URL out of a any URL from the same
web site
For instance if I'm onhttp://www.regular-expressions.inf o/javascriptexamp le.html
I want to extracthttp://www.regular-expressions.inf o
If you know your subject string is a well-formed URL then use the
regular expression from RFC3986:

^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?

$1 and $3 give you the scheme and authority, e.g., http://host.invalid

http://www.apps.ietf.org/rfc/rfc3986.html

--
Jock
Nov 30 '07 #5
On Nov 29, 11:25 pm, ZeldorBlat <zeldorb...@gma il.comwrote:
Why not make life easy and use this instead:
<http://www.php.net/parse-url>
Thanks! Exactly what I needed :)

--
Charles.
Nov 30 '07 #6
On Nov 30, 7:34 am, Toby A Inkster <usenet200...@t obyinkster.co.u k>
wrote:
preg_match('#ht tp://[\w.-]+#','http://www.regular-expressions.inf o/
javascriptexamp le.html',$match es);

That's much more readable, right?
Thanks Toby, incredible, but it works! :)

--
Charles.
Nov 30 '07 #7

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

Similar topics

3
2085
by: Jon Maz | last post by:
Hi All, Am getting frustrated trying to port the following (pretty simple) function to CSharp. The problem is that I'm lousy at Regular Expressions.... //from http://support.microsoft.com/default.aspx?scid=kb;EN-US;246800 function fxnParseIt() { var sInputString = 'asp and database';
3
35102
by: Jon Davis | last post by:
The date string: "Thu, 17 Jul 2003 12:35:18 PST" The problem: // this fails on PST DateTime myDate = DateTime.Parse("Thu, 17 Jul 2003 12:35:18 PST"); Help? Jon
20
8119
by: jeevankodali | last post by:
Hi I have an .Net application which processes thousands of Xml nodes each day and for each node I am using around 30-40 Regex matches to see if they satisfy some conditions are not. These Regex matches are called within a loop (like if or for). E.g. for(int i = 0; i < 10; i++) { Regex r = new Regex();
6
6540
by: Raed Sawalha | last post by:
Hello: I have a textbox which let user key in time.. sample of data: 06:00 AM 9:15 PM User only can key in the 12 hour format. how to write the Regular Expression for such input? Thanks!!!!
9
1528
by: Ron | last post by:
Hi, Can anyone help out with a regex for time? I would like the regex to accept: HH:MM AM HH:MM PM Optional space, optional 1st H, case doesn't matter. Thanks in advance.
6
2503
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);
7
2588
by: Extremest | last post by:
I am using this regex. static Regex paranthesis = new Regex("(\\d*/\\d*)", RegexOptions.IgnoreCase); it should find everything between parenthesis that have some numbers onyl then a forward slash then some numbers. For some reason I am not getting that. It won't work at all in 2.0
3
2702
by: aspineux | last post by:
My goal is to write a parser for these imaginary string from the SMTP protocol, regarding RFC 821 and 1869. I'm a little flexible with the BNF from these RFC :-) Any comment ? tests= def RN(name, regex): """protect using () and give an optional name to a regex""" if name:
5
13847
by: shapper | last post by:
Hello, What is the Regex expression to validate a date time format as follows: dd-mm-yyyy hh:mm:ss An example: 20-10-2008 10:32:45
0
9622
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
9454
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,...
0
10268
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...
1
10048
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
9916
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...
1
7464
isladogs
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5360
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...
1
4017
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
3
2853
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.