I am working on SEO thing in project to match best URL from possible url's, so
i am trying to match request url using preg_match() function from url pattern
can anyone please help me create regular expression to match only specific urls from all urls, i am newbie in Regular expression, please see my stuff
following 3 urls
1). http://domain.com/pressrelease/index/1
2). http://domain.com/pressrelease/123
3). http://domain.com/pressrelease/blah
i want to match 2,3 urls, urls have not contain of index/(:num) after pressrelease
i create this regular expression but it's does not working
(\/pressrelease\/)+((?!index).)*$
Since you're passing the regex to preg_match, the below regex would be fine.
\/pressrelease\/(?!index\/\d+\b).*
DEMO
(?!index\/\d+\b) negative lookahead assertion which asserts that the match /pressrelease/ won't be followed by the string which is in the format like index/number.
It actually works, but it doesn't match the first part of the URL.
^.*\/pressrelease\/(?!index).*$
Take a look at this demo on Rubular to check it.
Related
I'm using the following pattern to match URLs in a string:
$pattern = '%\b(([\w-]+://?|www[.])[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/)))%s';
This works pretty well. However, the match fails with URLs like this:
https://twitter.com/search/from:username(exclude:replies)min_faves:20
It seems to stop at the parentheses. Any ideas on how I could modify the pattern to match this type of URL? Thanks in advance!
Take the parentheses of of your negated character class and it works.
[^\s<>]+
https://regex101.com/r/4amF6u/1/
Full version:
$pattern = '%\b(([\w-]+://?|www[.])[^\s<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/)))%s';
I always have I kinda rough time working with regexes. I'm trying to make a regex that matches routes, when the route has parameters set:
For instance:
/post/1 matches /post/{id}
/post/5/ doesn't match /post/{id}
/post/6/comments/4 matches /post/{id}/comments/{comment}
/post/a-random-slug matches /post/{id} or /post/{slug} (whatever you want to name the param)
/user matches /user, but not /user/
What I currently did is create a regex for every route, and then match the current URI against that route regex.
What I currently have is:
My regex
In this example I try to make a regex for the route: /post/{param1}/{param2}. Meaning it should match /post/ then a parameter and another parameter, but nothing after that parameter.
As you can see: ^(\/post\b)(\/.{1,}\/)(.{1,}\b)$ matches /post/what-is-your-name/5, and when I add another / it doesnt match anymore. However if you add characters after that regex again it starts to match again.
Meaning that:
/post/what-is-your-name/5/ doesn't match
/post/what-is-your-name/5/more does match
Does anyone have an idea how I can accomplish the first example?
I'm by far someone who knows a lot about regexes, if someone sees a better way to match URIs against routes then please let me know.
Hope this will help you out
Regex: ^(?:\/post\b)(?:\/[\w]+){2}$
Regex demo
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.
I have a regex that's matching urls and converting them into html links.
If the url is already part of a link I don't want to to match, for example:
http://stackoverflow.com/questions/ask
Should match, but:
Stackoverflow
Shouldn't match
How can I create a regex to do this?
If your url matching regular expression is $URL then you can use the following pattern
(?<!href[\"'])$URL
In PHP you'd write
preg_match("/(?<!href[\"'])$URL/", $text, $matches);
You can use a negative lookbehind to assert that the url is not preceded by href="
(?<!href=")
(Your url-matching pattern should go immediately after that.)
This link provides information. The accepted solution is like so:
<a\s
(?:(?!href=|target=|>).)*
href="http://
(?:(?!target=|>).)*
By removing the references to "target" this should work for you.
Try this
/(?:(([^">']+|^)https?\:\/\/[^\s]+))/m
I have a PHP regular expression I'm using to get the YouTube video code out of a URL.
I'd love to match this with a client-side regular expression in JavaScript. Can anyone tell me how to convert the following PHP regex to JavaScript?
preg_match("#(?<=v=)[a-zA-Z0-9-]+(?=&)|(?<=v\/)[^&\n]+(?=\?)|(?<=embed/)[^&\n]+|(?<=v=)[^&\n]+|(?<=youtu.be/)[^&\n]+#", $url, $matches);
Much appreciated, thanks!
I think the only problem is to get rid of the lookbehind assertions (?<=...), they are not supported in Javascript.
The advantage of them is, you can use them to ensure that a pattern is before something, but they are NOT included in the match.
So, you need to remove them, means change (?<=v=)[a-zA-Z0-9-]+(?=&) to v=[a-zA-Z0-9-]+(?=&), but now your match starts with "v=".
If you just need to validate and don't need the matched part, then its fine, you are done.
But if you need the part after v= then put instead the needed pattern into a capturing group and continue working with those captured values.
v=([a-zA-Z0-9-]+)(?=&)
You will then find the matched substring in $1 for the first group, $2 for the second, $3 ...
you can replace your look behind assertion using this post
Javascript: negative lookbehind equivalent?