In some symfony2 project I have next definition of routes:
MyBundle_Default:
resource: "routing/default.yml"
prefix: /{slug}-{resource_id}
requirements:
slug: "[a-z\-]+"
resource_id: "\d+"
But this requirements don't work.
For example, if I have "-" in slug (e.g. "test-route") url like /test-route-9 do not work.
Is this a bug?
Mainly, I think your problem comes from the dash character. Let me explain. Say the url entered is /test-route-9. With your requirements, the slug matched would be test-route- because your regex states that is characters between a-z or the dash - that can be matched.
Regex will match every thing it can, so it will "eat" the dash character. By "eating" it, the rest would be only 9 which would not be correct because the routing expects another dash before matching the resource_id portion. Hence, this url wouldn't match as you expect.
Simple fix is to separate components with a / instead of dash, or change the character separating your slug to differentiate it from the url component separator.
Fix by changing separator in url:
prefix: /{slug}/{resource_id}
requirements:
slug: "[a-z\-]+"
resource_id: "\d+"
Or:
prefix: /{slug}_{resource_id}
requirements:
slug: "[a-z\-]+"
resource_id: "\d+"
Fix by changing separator for the slug directly:
prefix: /{slug}-{resource_id}
requirements:
slug: "[a-z_]+"
resource_id: "\d+"
I'm pretty sure the problem comes from this. Did not test anything, but worth a try just to be sure the problem lies with the dash character separator.
It's a typo, you put the requirement on hotel_id but define in the url resource_id!
Try with
slug: "[a-z\-]+(?<!-)"
This will make sure that the regex does NOT match the last dash, which is the problem as mentioned in Matts answer.
I hope it works for you :)
Looks like it was indeed a bug. At least in Symfony 3.4.36, all works as expected.
Related
Path: /1,2,3,456,678 - only numbers and commas, not anything else
Should be matched with regex-like path like this: /ids:(\\d+[,\\d]*) natively
But https://www.npmjs.com/package/path-to-regexp in express compiles it to some ridiculous regex
Expressers/noders - pls guide me how to approach this right
Replacing * with {0,} solved the issue
So request /1,2,34,56 matched by path: /ids:(\\d+[,\\d]{,*})
Link to path-to-regexp issue: https://github.com/pillarjs/path-to-regexp/issues/233
in node js when i try to check for validation of incoming string using express-validator it doesn't match using
check('firstName').matches('^[a-zA-Z\s\'\-$]')
to parse firstName of incoming request body
Note I've edited the question to be like
check('firstName').matches('^[a-zA-Z\s\'\-]$')
I see two issues here:
The range issue because of \-. You should use double escaping character instead.
The given regex will only match the first character because the quantifier is missing. You should use the + (one or more characters) quantifier at the end of the regex for full match.
The correct regex for your case would be:
check('firstName').matches('^[a-zA-Z\s\'\\-$]+')
express was treating the string in different way than /regex/ this was the issue.
I'm attempting to use Regex to extract a sub-domain from a url that follows a strict pattern. I want to only match urls with subdomains specified, so I'm using a negative look-ahead. This seems to work in many regex evaluators, but when I run in node, both strings get matched. Here's the code:
const defaultDomain = 'https://xyz.domain.com';
const scopedDomain = 'https://xyz.subdomain.domain.com';
const regex = /^https:\/\/xyz\.([^.]+(?!domain))\./
const matchPrefix1 = defaultDomain.match(regex);
const matchPrefix2 = scopedDomain.match(regex);
console.log(matchPrefix1);
console.log(matchPrefix2);
Expected: matchPrefix1 is null and matchPrefix2 results in a match where the first capture group is 'subdomain'
Actual: both matchPrefix1 and matchPrefix2 contain data, with the capture groups coming back as 'domain' and 'subdomain' respectively
Link to regexr (works!): https://regexr.com/42bfn
Link to repl (does not work): https://repl.it/#tomismore/SpiffyFrivolousLaws
What's going on here?
Regexr shows your code working because you didn't add the multiline flag. This causes the start of the second line to not match ^, so the whole second line is ignored. Add the multiline flag to see your regex not working.
I would use this regex:
^https:\/\/xyz\.(?!domain)([^.]+)\.
The change I made is to move the [^.]+ part to after checking (?!domain). Basically, you should check for (?!domain) immediately after matching xyz\..
I am trying to URL Rewrite to the following URL
Http://***.com/index.php?p=forum&mod=view_posts&page=$3&name=$2&id=$1
Http://***.com/forum/{id}-{name}/{page}
Http://***.com/forum/1-Hello-World/1
I have tryed the following code and have had no joy
RewriteRule ^forum/([^-]+)-([^&]+)/([^-]+)$ index.php?p=forum&mod=view_posts&page=$3&orderby=$2&id=$1
Thanks
That regex isn't very good: you see, the "([^&]+)" says: "one or more characters, up until the first ampersand", while you have no ampersands in the subject. Also, the "([^-]+)$" says "one or more characters before a hyphen", while you don't intend to end the subject with a hyphen.
Try this one:
^forum/([^-]+)-([^/]+)/(.+)$
But note that this actually captures any characters in the id and page positions, so you might be better off with
^forum/([0-9]+)-([^/]+)/([0-9]+)$
as that allows only numbers in those positions.
Also, you probably meant "index.php?p=forum&mod=view_posts&page=$3&name=$2&id=$1" instead of "index.php?p=forum&mod=view_posts&page=$3&orderby=$2&id=$1"
I need to rewrite a url like test.php?type=$1&val=$2
type will always be a string where as val could be a number or a string. I came up with the following
test/([a-zA-Z]+)/([a-zA-Z0-9|]+)/([0-9]+).html but as it was expected it will not work with something lke test/hello-world/23.html How can i included dashes in the expression?
You can escape them, so if you had
test/([a-zA-Z]+)/([a-zA-Z0-9\-|]+)/([0-9]+).html
This seems more like a question about regular expressions to me. test/([a-zA-Z-]+)/([a-zA-Z0-9|-]+)/([0-9]+).html should do. It's important to put the dash at the end of the character class.