I have the following in my routes.php:
Route::get('test/foo', 'TestController#index');
Route::get('test/bar', 'TestController#index');
Route::get('test/baz', 'TestController#index');
and I am trying to reduce this to the following:
Route::get(either 'test/foo' or 'test/bar' or 'test/baz', 'TestController#index');
One documented approach that would sort of apply here, is to place a regex constraint to the route:
Route::get('test/{uri}','TestController#index')->where('uri', 'regex for foo, bar, and baz...');
However, this solution would be ugly. Isn't there an elegant way to just express
{uri} in foo,bar,baz
in Laravel's language? Otherwise, what would the regex look like?
Thanks!
P.S. I've read this, but it didn't apply to my case with 3 routes.
I'm not sure why do you say that RegEx is ugly. I basically think RegEx is one of the most powerful tools.
In your case, I think the below snippet should do the job:
Route::get('user/{name}', function ($name) { // }) ->where('name', '(foo|bar|baz)');
The (foo|bar|baz) RegExr will match any of these string: 'foo', 'bar', 'baz'. So, if you need more, just add pipe (|) and add the needed string.
Related
I have a dropdown of vehicle makes that I want my users to start selecting as they type. First character typed should find the first character in the makes. The problem is that it searches anywhere in the make for a character and does not start at the first character like my users would like. For example... if you type an "r" you get: Alfa Romeo, Aston Martin, Chevrolet, Chrysler, etc... well before you get a Renault.
I create my list from the database. My haml looks like this:
.field-row
= render partial:'/makes/make_select', locals:{id:'make_id'}
That calls this _make_select.html.haml
= collection_select :vehicle, id, Make.all.order(:name), :id, :name, {prompt:true}, {title:'Select Make', class:'make-select', 'data-allow-empty' => 'no'}
I cant seem to find any docs on Ruby that shows me the valid options for collection_select. Maybe there is an option that allows this?
I have read that I might need to use jQuery to accomplish this. Was just trying to figure out if there might be an easier way with just a simple option in the haml.
Let me know if there is anything else you would like to see.
thanks!
You could add logic to your controller and to check using a SQL query, something like this, depends on the database you're using.
#makes = Make.where('name LIKE ?', "#{params[character]}%")
Check out the MySql docs on pattern matching
https://dev.mysql.com/doc/refman/8.0/en/pattern-matching.html
You should not add your Model query inside a view, should add it.
In your above case, I suggest you use https://github.com/argerim/select2-rails it very powerful and already have what you need.
I have a a number of routes that can be like :
possible routes:
- mac-book-retina-17-pid234-234
- hp-laptop-pid234-234
- vaoe-x12-pid234-234
and I want to match all to one action using the constraints in Ruby route file. Something like
get 'products/:product_info', to: 'products#type', constraints: { product_info: /[a-z]+-a-z]+-a-z]+-pid\d+-\d+/ }
The problem is that the /[a-z]+-/ can get repeated 1 time, 2 times and 3 times, and it makes it hard to get a consistent shared Regex for all the cases.
The only part that is constant in all routes is the last part: pid234-234 which refers to the product id and another sub_id.
I am thinking of something like: find all strings untill you each this part(pid), but I do not know how to do that.
I would say a good place to start is dynamic-segments
get 'products/:product_info', to: 'products#type', constraints: { product_info: /[A-Z]\d{5}/ }
I hope that this helps
Happy Hacking
I think I managed to find a possible solution for this:
(.*)pid\d+-\d+
this regex will match all the strings until it reaches the pid-12-12.
I'm building the new version of a website and I have to take charge of older urls looks like :
http://website.com/article_title_rewrited-article_id.html
Actually, I try to work about something like that :
app.get('/:title\-:id([0-9]).html', function...);
But of course it fails !
Can I do this type of rewriting using expressjs, or have I to use another method to port the url rewriting ?
Thanks by advance !
I see you are trying to use the Express url route parser as well as REGEX expressions, unfortunately this doesn't work, you have to use one or the other.
Remove the string and place with a REGEX pattern. Then the groups in the REGEX expressions will be available at req.params[0] and req.params[1].
app.get(/(.+)\.html/, function(req, res, next) {
res.redirect(req.params[0].substring(0, req.params[0].length - 5)); // -5 for length of '.html'
});
I believe that should (untested) sort it generically for all .html extensions if that can be a helpful guide :)
I'm new to using regex expressions. I need to accept all subdomains like:
something.mysite.com
something2.mysite.com
anotherthing.mysite.com
What kind of regex can I put there if I want to do something like:
rack_env['SERVER_NAME'].match <regex>
You shouldn't be using a regex here. The way to go is:
rack_env['SERVER_NAME'].end_with?(".mysite.com")
Something along the lines of \.mysite\.com$ should work. http://rubular.com is a good resource for testing regular expressions.
[a-zA-Z0-9]+\.[a-z]+\.com
something.mysite.com //ok
something2.mysite.com //ok
anotherthing.mysite.com //ok
something2mysite.com //not ok
anotherthing.mysitecom //not ok
But It is risky because you.can.have.as.many.subdomain.as.you.want in the future
If it just the sub domains that are changing you could use:
/\w+\.mysite\.com/
I'm currently building a code generator, which aims to generate boiler plate for me once I write the templates and/or translations, in whatever language I have to work with.
I have a problem with a regex in Ruby. The regex aims to select whatever is between {{{ and }}}, so I can generate functions according to my needs.
My regex is currently :
/\{\{\{(([a-zA-Z]|\s)+)\}\}\}/m
My test data set is:
{{{Demande aaa}}} => {{{tagadatsouintsouin tutu}}}
The results are:
[["Demande aaa", "a"], ["tagadatsouintsouin tutu", "u"]]
Each time the regex picks the last character twice. That's not exactly what I want, I need something more like this:
/\{\{\{((\w|\W)+)\}\}\}/m
But this has a flaw too, the results are:
[["Demande aaa}}} => {{{tagadatsouintsouin tutu", "u"]]
Whereas, I wish to get:
[["Demande aaa"],["tagadatsouintsouin tutu"]]
How do I correct these regexes? I could use two sets of delimiters, but it won't teach me anything.
Edit :
All your regex run against my data sample, so you all got a point.
Regex may be overkill, and probably are overkill for my purpose. So i have two questions.
First, do the regex keep the same exact indentation ? This should be able to handle whole functions.
Second, is there something fitter for that task ?
Detailled explanation of the purpose of this tool. I'm bored to write boiler plate code in php - symfony. So i wish to generate this according to templates.
My intent is to build some views, some controllers, and even parts of model this way.
Pratical example : In my model, i wish to generate some functions according to the type of an object's attribute. For examples, i have functions displaying correctly money. So i need to build the corect function, according to my attribute, and then put in , inside m output file.
So there is some translations which themselves need translations.
So a fictive example :
{{{euro}}} => {{{ function getMyAttributeEuro()
{
return formating($this->get[[MyAttribute]]);
} }}}
In order to stock my translations, should i use regex, like
I wish to build something a bit clever, so it can build most of the basic code with no bug. So i can work on interesting code.
You have one set of capturing parentheses too many.
/\{\{\{([a-zA-Z\s]+)\}\}\}/
Also, you don't need the /m modifier because there is no dot (.) in your regex whose behaviour would be affected by it.
I'm partial to:
data = '{{{Demande aaa}}} => {{{tagadatsouintsouin tutu}}}'
data.scan(/\{{3}(.+?)}{3}/).flatten.map{ |r| r.squeeze(' ') }
=> ["Demande aaa", "tagadatsouintsouin tutu"]
or:
data.scan(/\{{3}(.+?)}{3}/).flatten.map{ |r| [ r.squeeze(' ') ] }
=> [["Demande aaa"], ["tagadatsouintsouin tutu"]]
or:
data.scan(/\{{3}(.+?)}{3}/).map{ |r| [ r[0].squeeze(' ') ] }
=> [["Demande aaa"], ["tagadatsouintsouin tutu"]]
if you need the sub-arrays.
I'm not big on trying to everything possible inside the regex. I prefer to keep it short and sweet, then polish the output once I've found what I was looking for. It's a maintenance issue, because regex make my head hurt, and I stopped thinking of them as a macho thing years ago. Regex are a very useful tool, but too often they are seen as the answer to every problem, which they're not.
Some people, when confronted with a problem, think “I know,
I'll use regular expressions.” Now they have two problems.
-- Jamie Zawinski
You want non capturing groups (?:...), but here is another way.
/\{\{\{(.*?)\}\}\}/m
Just a shot
/\{\{\{([\w\W]+?)\}\}\}/
Added non-greedyness to your regex
Here this seems to work