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
Related
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 need to match a few urls for an application I'm working on;
So, I've got this reference string:
content/course/32/lesson/61/content/348
and I need a pattern that matches either
content
OR
content/course/[number]/lesson/[number]/content/[number]
What I've done so far is come up with this pattern:
$my_regex = "/content(\/?|(\/course\/\d{1,4}\/lesson\/\d{1,4}\/content\/\d{1,4}))$/";
which however has the following problem: This string returns a match which should otherwise not:
content/course/32/lesson/61/content
I'm thinking that it's got something to do with the word content repeating twice but I'm not entirely sure.
Any help is much appreciated.
The reason for the match is the alternation.
content\/?$
matches
content/course/32/lesson/61/content
To fix this, add a ^ (beginning of line) to the start of your regex to ensure the entire string is matched and not only the ending:
/^content(\/?|(\/course\/\d{1,4}\/lesson\/\d{1,4}\/content\/\d{1,4}))$/
See it in action
this works:
/(^content\/?|content\/course\/\d{1,4}\/lesson\/\d{1,4}\/content\/\d{1,4})$/
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.
I'm trying to create a snippet of regex that will match a URL route.
Basically, if I have this route /users/:id I want /users/100 to match, but /users/100/edit not to match.
This is what I'm using now: users/(.*)/ but because of the greedy match it's matching regardless of what's after the user ID. I need some way of "breaking" the match if there's an /edit or something else on the end of the route.
I've looked into the Regex NOT operator but with no luck.
Any advice?
Are you just trying to collect digits?
You could use users/(\d*)/
And this one is how you would do it if you wanted to collect everything until a /, and it uses a NOT, ^/users/[^/]*$
You can use negative lookahead:
users/(.*)/(?!edit)
This will always require a trailing slash however. Maybe a better solution would be:
users/(\d+)(?!/edit)
See this post for more information.
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