I have the following strings in my application.
/admin/stylesheets/11
/admin/javascripts/11
/contactus
what I want to do is to write a regular expression to capture anything other than string starting with 'admin'
basically my regex should capture only
/contactus
by excluding both
/admin/stylesheets/11
/admin/javascripts/11
to capture all i wrote
/.+/
and i wrote /(admin).+/ which captures everything starts with 'admin'. how can i do the reverse. I mean get everything not starting with 'admin'
thanks in advance
cheers
sameera
EDIT - Thanks all for the answers
I'm using ruby/ Rails3 and trying to map a route in my routes.rb file
My original routes file is as followss
match '/:all' => 'page#index', :constraints => { :all => /.+/ }
and i want the RegEx to replace /.+/
thanks
If the language/regular expression implementation you are using supports look-ahead assertions, you can do this:
^/(?!admin/).+/
Otherwise, if you only can use basic syntax, you will need to do something like this:
^/([^a].*|a($|[^d].*|d($|[^m].*|m($|[^i].*|i($|[^n].*)))))
Related
I'm trying to match a route that has the keywords -episode or -movie.
Such as /steins-gate-episode-1 or /pokemon-movie-10
I tried doing this:
$app->get('/{slug:episode|movie}', \App\Controller\EpisodeController::class . ':getBySlug');
But it isn't matching.
Any help would be appreciated. I am completely new to this btw.
Your regular expression matches only the exact strings "episode" and "movie". If you want to check if the URL contains the substrings, you can use this:
$app->get('/{slug:.*episode.*|.*movie.*}', function ($request, $response, $args) {
echo $args['slug'];
});
.* means "any number of any character" . Sure are there more advanced regexp patterns but that will do what you need.
I have a laravel validation rule something like this
public function rules(){
return [
'title' => 'required|max:100',
'featured_image' => 'required|max:100|regex:(\d)+.(?:jpe?g|png|gif)',
];
}
I have a txt field where i dynamically add an image name, something like this (8123123123.jpg OR 234234234.png). If the text field doesn't have this pattern i want to show an error.
Now this regex does work in http://regexr.com/ but in laravel it doesn't. So basically it should look for digits as file name and should end with .jpg or .png
Any help is appreciated
use like this
'featured_image' => ['required', 'max:100', 'regex:/(\d)+.(?:jpe?g|png|gif)/']
You have to add a regex delimiter http://php.net/manual/en/regexp.reference.delimiters.php
According to the docs you should put your validations in a array:
regex:pattern
The field under validation must match the given regular expression.
Note: When using the regex pattern, it may be necessary to specify rules in an array instead of using pipe delimiters, especially if the regular expression contains a pipe character.
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)
I'm trying to create a regex that takes a filename like:
/cloud-support/filename.html#pagesection
and redirects it to:
/cloud-platform/filename#pagesection
Could anyone advise how to do this?
Currently I've got part-way there, with:
"^/cloud-support/(.*)$" => "/cloud-platform/$1",
which redirects the directory okay - but still has a superfluous .html.
Could I just match for a literal .html with optional #? How would I do that?
Thanks.
Maybe something like this:
"^/cloud-support/(.*?)(\.html)?(#.+)$" => "/cloud-platform/$1$3"
where the first group is a non-greedy match (.*?)
"^/cloud-support/(\w+).html(.*)" => "/cloud-platform/$1$2"
Would something like this work?
"^/cloud-support/([^.]+)[^#]*(.*)$" => "/cloud-platform/$1$2"
Can you try the regex
"^/cloud-support/(.*)\.html(#.*)?$"
The \.html part matches .html while (#.*)? allows an optional # plus something.
I would like some help matching the following urls.
/settings => /settings.php
/657_46hallo => /657_46hallo.php
/users/create => /users.php/create
/contact/create/user => /contact.php/create/user
/view/info.php => /view.php/info.php
/view/readme - now.txt => /view.php/readme - now.txt
/ => [NO MATCH]
/filename.php => /unknown.php
/filename.php/users/create => /unknown.php
if the first part after the domain name is a filename ending with ".php"
(see last 2 examples) It should redirect to /unknown.php
I think I need 2 regular expressions
1st should be almost something like: ^/([a-zA-Z0-9_]+)(/)?(.*)?$
2nd to catch the direct filename "/filename.php" or "/filename.php/create/user"
so I can redirect to unknown.php
The 1st regular expression that I got almost works for the first part.
==============================================
request url: http://domain.com/user/create
regex: ^/([a-zA-Z0-9_]+)(/)?(.*)?$
replace http://domain.com/$1.php$2$3
makes: http://domain.com/user.php/create
Problem is it also matches http://domain.com/user.php/create
If someone could help me with both regular expressions that would be great.
If you want to match those .php cases you can try this:
^\/([a-zA-Z0-9_]+)(\/)?(.*)?$
See here on Regexr
If you want to avoid those cases try this:
^/([a-zA-Z0-9_]+)(?!\.php)(?:(/)(.*)|)$
See here on Regexr
The (?!\.php) is a negative look ahead that ensures that there is no .php at this place.
When all you have is a hammer...
While this probably could be solved with a regexp, it is probably the wrong tool for the job, unless you have constraints that MANDATE the use of regexps.
Split the string using '/' as the delimiter, see whether the first component ends with '.php'; if so, reject it, otherwise append '.php' to the first component and join the components back using '/'.