I’m chasing an odd pattern matching issue in labstack/echo and would like your thoughts.
Would the route
/first/:parameter/second
match the url
http://hostname/first or http://hostname/first/ ?
What about
/first/:parameter1/second/:parameter2/:parameter3/third/ ?
To my eye, they should not match the simple urls, but they appear to be. Is that desired behavior? Has anyone followed a parameter with a static in the url pattern?
In Echo your routes need to be in an order to not match. See the Routing Guide here and look for Path Matching Order
https://echo.labstack.com/guide/routing
Switch around your routes so your /first/:parameter/second doesn't match /first/:parameter1/second/:parameter2/:parameter3/third/
Related
Lets suppose I have a url like this:
https://www.youtube.com/watch/3e4345?v=rwmEkvPBG1s
What is the best and shorthest way to only get the 3e4345 part?
Sometimes it doesn't contain additional params in ?
I don't want to use any gems.
What I did was:
url = url.split('/watch/')
url = url[1].split('/')[0].split('?')[0]
Is there a better way? Thanks
possibly the safest and best one. use URI.
URI("https://www.youtube.com/watch/34345?v=rwmEkvPBG1s").path.split("/").last
For more refer How to extract URL parameters from a URL with Ruby or Rails?
You could do the following and using the match function to find a match based on a regular expression statement. The value at [1] is the first capture from the regular expression. I have included a breakdown from regexper.com to help illustrate what the expression is accomplishing.
You will notice parentheses around the \d+ which are what captures the digits out of the URL when it matches.
url.to_s.match(/\/watch\/(\d+).*$/)[1]
x = "https://www.youtube.com/watch/34345?v=rwmEkvPBG1s"
File.basename(URI(x).path)
=> "34345"
I am creating a custom uri for my application. I wanted to know if there are any rules on the name of URI .. ie. can i use myApp_1.0 as an uri?
Ok got the answer, URI schemes can only contain letters, digits, plus ("+"), period ("."), or hyphen ("-"). also the URI is case insensitive, but it is recommended to use lower case only.
More details can be found at http://en.wikipedia.org/wiki/URI_scheme
I have been struggling with the same issue for a while now and I could not find an good answer yet. I'm using rack-rewrite to add some url rewrite rules to my app's middleware stack.
I have the following rule:
r301 %r{^/([^(docs|help|legal|login|account|apps)])(.+)/$}, '$1'
Which is not working properly or as I would expect it. I have tried one of my previous question's answer, but neither that works, it actually generates an event more weird behaviour (it redirects to an url without the domain name, just to the path).
What I am trying to do is:
if user requests http://example.com/ or http://example.com/random-path/ I need the rewrite rule to strip the slash, thus the examples would become http://example.com respectively http://example.com/random-path;
if the requested paths matches any of the paths in the list docs|help|legal|login|account|apps, do not strip the slash at the end of the path if exists, but add a slash if it's not there
I tried with two rules, one that ignores the listed paths above and strips slashes and one that adds the slash if it hits something from the list and the slash after the path is not there:
r301 %r{^/([^(docs|help|legal|login|account|apps)])(.+)/$}, '/$1'
r301 %r{^/([(docs|help|legal|login|account|apps)])(.+)/$}, '/$1/'
How could I write a rule that would do that, or two rules, because what I tried it did not work?
You can do that like so:
r301 %r{^/((?!docs|help|legal|login|account|apps).+)/$}, '/$1'
r301 %r{^/((?=docs|help|legal|login|account|apps).+[^/])$}, '/$1/'
example 1
example 2
and some documentation on lookahead and lookbehind
EDIT: stray parentheses.
I need to identified urls that are either "/fr/" or "/en/". (and only these two)
I'm looking for the good regular expression.
Of course it works if I write "/../", but it's too large.
Best I could find is "/[fe][rn]/" but it also take /fn/ and /er/.
Simply use a pipe in a group :
/(fr|en)/?
Edit: added the optional trailing slash
I am trying to construct a regex to extract a domain given a url.
for:
http://www.abc.google.com/
http://abc.google.com/
https://www.abc.google.com/
http://abc.google.com/
should give:
abc.google.com
URI.parse('http://www.abc.google.com/').host
#=> "www.abc.google.com"
Not a regex, but probably more robust then anything we come up with here.
URI.parse('http://www.abc.google.com/').host.gsub(/^www\./, '')
If you want to remove the www. as well this will work without raising any errors if the www. is not there.
Don't know much about ruby but this regex pattern gives you the last 3 parts of the url excluding the trailing slash with a minumum of 2 characters per part.
([\w-]{2,}\.[\w-]{2,}\.[\w-]{2,})/$
you may be able to use the domain_name gem for this kind of work. From the README:
require "domain_name"
host = DomainName("a.b.example.co.uk")
host.domain #=> "example.co.uk"
Your question is a little bit vague. Can you give a precise specification of what it is exactly that you want to do? (Preferable with a testsuite.) Right now, all your question says is that you want a method that always returns 'abc.google.com'. That's easy:
def extract_domain
return 'abc.google.com'
end
But that's probably not what you meant …
Also, you say that you need a Regexp. Why? What's wrong with, for example, using the URI class? After all, parsing and manipulating URIs is exactly what it was made for!
require 'uri'
URI.parse('https://abc.google.com/').host # => 'abc.google.com'
And lastly, you say you are "trying to extract a domain", but you never specify what you mean by "domain". It looks you are sometimes meaning the FQDN and sometimes randomly dropping parts of the FQDN, but according to what rules? For example, for the FQDN abc.google.com, the domain name is google.com and the host name is abc, but you want it to return abc.google.com which is not just the domain name but the full FQDN. Why?