1

I want to make a regular expression for this laravel route.

Route::get('/{id}', 'HomeController@profile')

So far I was able to accept number only and string only using ->where('id', '\w+'); and )->where('id', '\d+');

But, I would like to make the rule so it accepts strings from 0-9, hyphen - and any string from A-Z or a-z

How can I do this. I haven't done any regex before.

1 Answer 1

1

The regex you're looking for is: [0-9A-Za-z\-]+

Sign up to request clarification or add additional context in comments.

4 Comments

Nope. It didn't work. Route::get('/{id}', 'HomeController@profile')->where('id', '[0-9A-Za-z\-]');
Try adding the + character in the end.
It seems to work. I will accept it in 5 minutes. One more question, what rule would I need to make it so, that it does not start with - ?
Try this: [0-9a-zA-Z][0-9a-zA-Z\-]*

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.