Why doesn't this URL pattern match? - php

I'm using a pattern as described by John Gruber in this daringfireball article to auto link URLs in user comments.
I'm using it with PHP to match URLs, and want it to match a single TLD with no www and no trailing slash, but it doesn't seem to be working.
Here's the pattern (and can be seen in more detail at the article above):
$pattern = '#(?i)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4})(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:\'".,<>?«»“”‘’]))#';
Specifically I'm looking at this particular subpattern: [a-z0-9.\-]+[.][a-z]{2,4}
This subpattern works separately, but as a part of the larger pattern, it doesn't match google.com.

[a-z0-9.\-]+[.][a-z]{2,4} works as you expect, but the rest of the pattern requires at least 1 following character:
google.com/
google.com?lang=en-us
google.com#!foo/bar
etc.
You can try allowing the tail to be optional, but it may in turn give you false-positives rather than excluding false-negatives:
$pattern = '#...“”‘’])?)#'; # '...' for brevity
# ^

Works for me:
http://regexr.com?2uica
Are you sure there is nothing in you php that is tripping you up?
EDIT
It's because the full pattern expects to find something before the domain name, like http:// or www

Related

regex to match a URL with optional 'www' and protocol

I'm trying to write a regexp.
some background info: I am try to see if the REQUEST_URI of my website's URL contains another URL. like these:
http://mywebsite.com/google.com/search=xyz
However, the url wont always contain the 'http' or the 'www'. so the pattern should also match strings like:
http://mywebsite.com/yahoo.org/search=xyz
http://mywebsite.com/www.yahoo.org/search=xyz
http://mywebsite.com/msn.co.uk'
http://mywebsite.com/http://msn.co.uk'
there are a bunch of regexps out there to match urls but none I have found do an optional match on the http and www.
i'm wondering if the pattern to match could be something like:
^([a-z]).(com|ca|org|etc)(.)
I thought maybe another option was to perhaps just match any string that had a dot (.) in it. (as the other REQUEST_URI's in my application typically won't contain dots)
Does this make sense to anyone?
I'd really appreciate some help with this its been blocking my project for weeks.
Thanks you very much
-Tim
I suggest using a simple approach, essentially building on what you said, just anything with a dot in it, but working with the forward slashes too. To capture everything and not miss unusual URLs. So something like:
^((?:https?:\/\/)?[^./]+(?:\.[^./]+)+(?:\/.*)?)$
It reads as:
optional http:// or https://
non-dot-or-forward-slash characters
one or more sets of a dot followed by non-dot-or-forward-slash characters
optional forward slash and anything after it
Capturing the whole thing to the first grouping.
It would match, for example:
nic.uk
nic.uk/
http://nic.uk
http://nic.uk/
https://example.com/test/?a=bcd
Verifying they are valid URLs is another story! It would also match:
index.php
It would not match:
directory/index.php
The minimal match is basically something.something, with no forward slash in it, unless it comes at least one character past the dot. So just be sure not to use that format for anything else.
To match an optional part, you use a question mark ?, see Optional Items.
For example to match an optional www., capture the domain and the search term, the regular expression could be
(www\.)?(.+?)/search=(.+)
Although, the question mark in .+? is a non-greedy quantifier, see http://www.regular-expressions.info/repeat.html.
You might try starting your regex with
^(http://)?(www\.)?
And then the rules to match the rest of a URL.
$re = '/http:\/\/mywebsite\.com\/((?:http:\/\/)?[0-9A-Za-z]+(?:-+[0-9A-Za-z]+)*(?:\.[0-9A-Za-z]+(?:-+[0-9A-Za-z]+)*)+(?:\/.*)?)/';
https://regex101.com/r/x6vUvp/1
Obeys the DNS rule that hyphens must be surrounded. Replace http with https? to allow https URLs as well.
According to the list of TLDs at Wikipedia there are at least 1519 of them and it's not constant so you may want to give the domain its own capture group so it can be verified with an online API or a file listing them all.
Here is my two cents :
$regex = "/http:\/\/mywebsite\.com\/((http:\/\/|www\.)?[a-z]*(\.org|\.co\.uk|\.com).*)/";
See the working exemple
But I'm sure you can do better !
Hope it helps.

Regex for detecting a word enclosed but in order

I have a regex that needs to match up a specific url and load some configuration based on that.
Basically What I am have is /*[^(search)].php/ This regex needs to match every url that has .php in the end but parameters may be present (something.pgp?t=19) except for the urls that have search.php
For example
1. http://www.example.com/discuss/viewtopic.php?t=19
2. http://www.example.com/discuss/viewforum.php?f=8
3. http://www.example.com/discuss/search.php?f=8
Among the above three urls the regular expression needs to be able to match 1 & 2 but not 3.
Any help is much appreciated thanks.
EDITED
However it should not be matching any other urls that does not include .php in it.
www.example.com/something should not be matched.
try this:
/.+?(?<!search)\.php(?<params>.*)/
the key is the non-greedy 'anything' .+? : it crawls up the string one by one, always checking for "look behind you and don't see 'search', followed by .php: (?<!search)\.php, followed by the named group which are the optional query string params.
Note that this simple regex is pretty permissive, and assumes that .php alone signifies a "php URL" - you could get crazy complicated validating URL's.
Just use two different location (make sure search.php is before general *.php)
location ~ /search\.php$ {
# config for phusion-passenger
}
location ~ \.php$ {
# config for php-fpm
}
Nginx strip off request parameters while searching for match, so you don't have to care about them.
Problem in /*[^(search)].php/
[^ ] negates of character class
so [^(search)] here would match anything other than ( or s or e or a etc
Solution
You can use a look behind assertion as
^.*(?<!search\.php)\?([^=]=)+\d+$
will match 1 and 2
Example : http://regex101.com/r/bJ3vG1/4
What it doess?
(?<!search\.php) negative lookbehind. asserts that the regex is presceded not by search.php
\?[^=]=\d+ matches parameters
Edit
If the parameter part is optional, a lengthier regex would do the purpose
.*(?<!search\.php)\?([^=]=)+\d+$|^[^?]*(?<!search\.php)$
Example : http://regex101.com/r/bJ3vG1/3

URL detection and BB-Style tags (regex, look-ahead issue)

so I'm building a small CMS and I'd like to avoid allowing HTML in the content editor. For that reason I want to detect raw URLs in text aswell as supporting BB-like tags, for better customization.
www.example.com
[link http://www.example.com]Click me[/link]
Unfortunately I'm fairly new to regular expressions and I just can't seem to get this working. I'm running two regular expressions over the string: The first detects raw URLs, the second BB-like URLs. The latter seems to work perfectly fine, the first one interferes though, and converts URLs wrapped in tags too.
I started off with a piece of code I found here and made some additions.
This is the code for non-tag URLs:
/* don't match URLs preceeded by '[link ' */
(?<!\[link\s)
(
/* match all combinations of protocol and www. */
(\bhttps?://www\.|\bhttps?://|(?<!//)\bwww\.)
/* match URL (no changes made here) */
([^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/)))
/* but don't match if followed by [/link] - THIS DOESN'T WORK */
(?!\[/link\])
)
The negative look-behind before the www. is there because / isn't a word character, and without it something like
[link http://www.example.com]example[/link]
would still match after http://.
The regex above produces the following matches (tested with http://gskinner.com/RegExr/, matches are in bold. I had to add spaces after http://because I'm not allowed to post more URLs):
www.example.com
http:// www.example.com
http:// example.com
[link http://www.example.com]no problem 1[/link]
[link www.example.com]no problem 2[/link]
[link http://www.example.com]http://www.example.com[/link]
I've tried moving the negative look-ahead around and played with the parentheses (pretty aimlessly), without success.
For completeness, here's the tag-matching regex (which seems to work):
(?:\[link\s)(\bhttps?://|\bwww\.|\bhttps?://www\.)([^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/)))\](.*)(?:\[/link\])
I'm sure someone can spot the error immediately.
Thanks a lot in advance!
I have taken your regex, insterted it into regexr with the examples you have given and tried to make it work.
Step by step:
1) The original regex: http://regexr.com?33snj. The problem why this regex also matches the [/link] is in the URL matching bit:
[^\s()<>]+
This will also match the open bracket character '[', therefore matching will not stop when it encounters the [/link] bit. It could be argued that the [ character is a valid URI character, but that is only under rare conditions (see this stackoverflow post for more info).
2) I decided to continue with your regex, but added the open bracket char to the negated character list:
[^\s()<>[]+
This will get you into another problem. See http://regexr.com?33snp. Because of backtracking the engine now finds a way around the negative lookahead at the end.
3) Once you make the URL matching group atomic (by adding ?> to the start of the capture group) the engine stops backtracking and we have arrived at the desired outcome.
(?<!\[link\s)((\bhttps?://www\.|\bhttps?://|(?<!//)\bwww\.)(?>[^\s()<>[]+(?:\([\w\d]+\)|([^[:punct:]\s]|/)))(?!\[/link\]))
See it in action http://regexr.com?33sns.

Trying to get base url with reg ex

I need to match the "base" url, what I mean is:
Not match --> http://google.com
Not match --> http://www.google.com
Not match --> www.google.com
Match! --> google.com
I was trying to use a negative look behind to make sure there was no http:// or www, but it didn't seem to work correctly.
Do this has to be with only one regex?
You could have the first regex that will match all URLs found. Something like that:
\b.+?\.\w{2,4}\b
And then filter all matches and keep the ones that do not match the following:
^(http://|www)
although to be honest, I wouldn't use Regex unless it is strictly necessary for that.
Note:
You can always find a better regex to match the URLs. The thing here is that they may not start with http:// or www, so we can't restrict the regex so much. Be ready to have other matches that are not urls at all, like:
yesterday.but in I was there yesterday.but no one saw me

Need a good regex to convert URLs to links but leave existing links alone

I have a load of user-submitted content. It is HTML, and may contain URLs. Some of them will be <a>'s already (if the user is good) but sometimes users are lazy and just type www.something.com or at best http://www.something.com.
I can't find a decent regex to capture URLs but ignore ones that are immediately to the right of either a double quote or '>'. Anyone got one?
Jan Goyvaerts, creator of RegexBuddy, has written a response to Jeff Atwood's blog that addresses the issues Jeff had and provides a nice solution.
\b(?:(?:https?|ftp|file)://|www\.|ftp\.)[-A-Z0-9+&##/%=~_|$?!:,.]*[A-Z0-9+&##/%=~_|$]
In order to ignore matches that occur right next to a " or >, you could add (?<![">]) to the start of the regex, so you get
(?<![">])\b(?:(?:https?|ftp|file)://|www\.|ftp\.)[-A-Z0-9+&##/%=~_|$?!:,.]*[A-Z0-9+&##/%=~_|$]
This will match full addresses (http://...) and addresses that start with www. or ftp. - you're out of luck with addresses like ars.userfriendly.org...
This thread is old as the hills, but I came across it while working on my own problem: That is, convert any urls into links, but leave alone any that are already within anchor tags. After a while, this is what has popped out:
(?!(?!.*?<a)[^<]*<\/a>)(?:(?:https?|ftp|file)://|www\.|ftp\.)[-A-Z0-9+&#/%=~_|$?!:,.]*[A-Z0-9+&#/%=~_|$]
With the following input:
http://www.google.com
http://google.com
www.google.com
<p>http://www.google.com<p>
this is a normal sentence. let's hope it's ok.
www.google.com
This is the output of a preg_replace:
http://www.google.com
http://google.com
www.google.com
<p>http://www.google.com<p>
this is a normal sentence. let's hope it's ok.
www.google.com
Just wanted to contribute back to save somebody some time.
I made a slight modification to the Regex contained in the original answer:
(?<![.*">])\b(?:(?:https?|ftp|file)://|[a-z]\.)[-A-Z0-9+&#/%=~_|$?!:,.]*[A-Z0-9+&#/%=~_|$]
which allows for more subdomains, and also runs a more full check on tags. To apply this to PHP's preg replace, you can use:
$convertedText = preg_replace( '#(?<![.*">])\b(?:(?:https?|ftp|file)://|[a-z]\.)[-A-Z0-9+&#/%=~_|$?!:,.]*[A-Z0-9+&#/%=~_|$]#i', '\0', $originalText );
Note, I removed # from the regex, in order to use it as a delimiter for preg_replace. It's pretty rare that # would be used in a URL anyway.
Obviously, you can modify the replacement text, and remove target="_blank", or add rel="nofollow" etc.
Hope that helps.
To skip existing ones just use a look-behind - add (?<!href=") to the beginning of your regular expression, so it would look something like this:
/(?<!href=")http://\S*/
Obviously this isn't a complete solution for finding all types of URLs, but this should solve your problem of messing with existing ones.
if (preg_match('/\b(?<!=")(https?|ftp|file):\/\/[-A-Z0-9+&##\/%?=~_|!:,.;]*[A-Z0-9+&##\/%=~_|](?!.*".*>)(?!.*<\/a>)/i', $subject)) {
# Successful match
} else {
# Match attempt failed
}
Shameless plug: You can look here (regular expression replace a word by a link) for inspiration.
The question asked to replace some word with a certain link, unless there already was a link. So the problem you have is more or less the same thing.
All you need is a regex that matches a URL (in place of the word). The simplest assumption would be like this: An URL (optionally) starts with "http://", "ftp://" or "mailto:" and lasts as long as there are no white-space characters, line breaks, tag brackets or quotes).
Beware, long regex ahead. Apply case-insensitively.
(href\s*=\s*['"]?)?((?:http://|ftp://|mailto:)?[^.,<>"'\s\r\n\t]+(?:\.(?![.<>"'\s\r\n])[^.,!<>"'\s\r\n\t]+)+)
Be warned - this will also match URLs that are technically invalid, and it will recognize things.formatted.like.this as an URL. It depends on your data if it is too insensitive. I can fine-tune the regex if you have examples where it returns false positives.
The regex will produce two match groups. Group 2 will contain the matched thing, which is most likely an URL. Group 1 will either contain an empty string or an 'href="'. You can use it as an indicator that this match occurred inside a href parameter of an existing link and you don't have to do touch that one.
Once you confirm that this does the right thing for you most of the time (with user supplied data, you can never be sure), you can do the rest in two steps, as I proposed it in the other question:
Make a link around every URL there is (unless there is something in match group 1!) This will produce double nested <a> tags for things that have a link already.
Scan for incorrectly nested <a> tags, removing the innermost one

Categories