I've constructed a URL in Silex to match /tutorial or /tutorial/index or /tutorial/pageX where X is a number, but I'd also like it to match /tutorial/ and I'm struggling to make that happen. This is the route I have so far:
$app->get('/tutorial/{page}', 'app\\Controller\\Tutorial::index')
->assert('page', '(^(page[\d]+|index)$|^$)')
->value('page', 'index');
If I try that regex in regex101.com it works, having nothing there matches. So why doesn't it work in Silex and what can I use that will work?
Appologies if this is a duplicate but all my searches turned up similar things, but not this exact situation where the whole string must match or be nothing.
Try this regex:
^(page\d+|index|)$
It will match "pagen", "index", or "".
If you still do not achieve what you need, I guess the issue is with the app\Controller\Tutorial::index method that cannot handle empty match.
Related
I have data from two URLS that I need to combine using REGEX
/online-teaching
/online-teaching?fbclid
I have /(online-teaching)|(online teaching)
I can't figure out how to include the url with the ? and the one without.
Thanks!
How about something as simple as:
online-teaching(?:.+)?
Regex demo
Match online-teaching and anything that follows, if it exists (might need to constraint for specific characters instead of matching all with . to have a valid URL, but I'll leave that up to you).
I need to fix my url pattern:
/^((http(s)?(\:\/\/)){1}(www\.)?([\w\-\.\/])*(\.[a-zA-Z]{2,4}\/?)[^\\\/#?])[^\s\b\n|]*[^\.,;:\?\!\#\^\$ -]/
I thought this regex was ok, but it is not working for urls like: https://xx.xx (without www). 'www' should be optional ((www.)?). Where is the bug?
The problem is not in the (www\.)? part but that parts after that.
Take a look at the [^\\\/#?] and the [^\.,;:\?\!\#\^\$ -] parts.
So a valid URL would be https://xx.xx plus none of \/#? plus none of .,;:?!#^$_- making the url valid if you add those, for example https://xx.xx11.
I do advice you to not try to create your own regex because you are missing a lot!
For example, tlds like .amsterdam are valid. And why are you capturing so many groups?
Your regex as an image made with https://www.debuggex.com/:
I've a problem with django url, when I go to:
/cars/my_town/my_office/
It's ok and view run as expected, town="my_town" and office_name="my_office".
When I go to:
/car/my_town/my_office/my_var1/
I get an error.
When I print the vars in my views I get:
town :"my_town/my_office" and office_name:"my_var1"
My view looks like this:
def ListingCars(request,town,office_name,var1=None) :
My urls:
url(r'^cars/(?P<town>[\w|\W ]+)/(?P<office_name>[\w|\W ]+)/$', web_public_views.ListingCars, name='listingvo_cars'),
url(r'^cars/(?P<town>[\w|\W ]+)/(?P<office_name>[\w|\W ]+)/(?P<var1>[\w|\W ]+)/$', web_public_views.ListingCars, name='listingvo_cars_var1'),
SOLVE ... it wasn't a resolverurl problem, but a myfault problem ;) i didn't see a "รง" on my tags name url... that cause the problem... thx for help i clean up my regex and sorry
\W is not word, which will match a slash, you can most likely just omit that and use \w, although its not clear what your url's should match, if it is slugs then its most likely that you need [\w-]+
^cars/(?P<town>[\w-]+)/(?P<office_name>[\w-]+)/$
^cars/(?P<town>[\w-]+)/(?P<office_name>[\w-]+)/(?P<var1>[\w-]+)/$
What is actually happening is your first regex is matching your url so it never uses the second one, so most likely you could also fix this by switching the order of these urls so the second is first but I wouldn't recommend this.
My Regex skills a minimum, I have been trying for a while now to get this to work:
I need to match all urls in one domain, but one (the login one).
Example:
Match: domain.com/ANYTHING-GOES-HERE
but
Not Match: domain.com/login
I don't actually need to match the domain.com part because that's always the same, what comes after it.
I have tried:
(?!\/login)\/.*
\/.*[^login]
Neither one seems to work as desired.
Update:
I should have explained that this is done in PHP. I don't have control over the actual code that runs the regex, but I do have control over how many regex I can have. So I could have one regex that matches everything, and then have one regex that matches or not matches "/login"
You're almost there:
// javascript
r = /domain\.com\/(?!login).+/
r.test("domain.com/ANYTHING-GOES-HERE") // true
r.test("domain.com/login") // false
This also rejects "domain.com/login/foobar", if you want it to be accepted, modify the regex to be
r = /domain\.com\/(?!login$).+/
I'm somewhat new to ruby and have done a ton of google searching but just can't seem to figure out how to match this particular pattern. I have used rubular.com and can't seem to find a simple way to match. Here is what I'm trying to do:
I have several types of hosts, they take this form:
Sample hostgroups
host-brd0000.localdomain
host-cat0000.localdomain
host-dog0000.localdomain
host-bug0000.localdomain
Next I have a case statement, I want to keep out the bugs (who doesn't right?). I want to do something like this to match the series of characters. However, it starts matching at host-b, host-c, host-d, and matches only a single character as if I did a [brdcatdog].
case $hostgroups { #variable takes the host string up to where the numbers begin
# animals to keep
/host-[["brd"],["cat"],["dog"]]/: {
file {"/usr/bin/petstore-friends.sh":
owner => petstore,
group => petstore,
mode => 755,
source => "puppet:///modules/petstore-friends.sh.$hostgroups",
}
}
I could do something like [bcd][rao][dtg] but it's not very clean looking and will match nonsense like "bad""cot""dat""crt" which I don't want.
Is there a slick way to use \A and [] that I'm missing?
Thanks for your help.
-wootini
How about using negative lookahead?
host-(?!bug).*
Here is the RUBULAR permalink matching everything except those pesky bugs!
Is this what you're looking for?
host-(brd|cat|dog)
(Following gtgaxiola's example, here's the Rubular permalink)