Regex to match any character or fullstop? - regex

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.

Related

Django url regex hit end of url: *./something

I am geting 404 on different urls that end with the same string and instead of creating multiple redirects I would like to catch them all on the last string. It always appears at the same position, pattern goes like so:
/some-of-my-urls/the-same-string
No trailing slash there. I tried something like this:
url(r'^[a-zA-Z0-9_]+/the-same-string', redirect_func),
url(r'^./the-same-string', redirect_func),
But that doesn't work. Probably obvious for somebody with more regex knowledge, I am not very advanced. Anybody ideas?
You may use a negated character class [^/] to match any char but / and quantify it with a + quantifier that matches 1 or more repetitions:
r'^[^/]+/the-same-string'
See the regex demo.

How do I exclude a folder from a URL using Regex?

I need to use Regex to check for URLs that contain 'folder', in the following URL:
subdomain.domain.co.uk/section/folder/page
I'm using:
subdomain.domain.co.uk\/.*\/(?!folder\/).*
but it's still finding 'folder'. Any ideas?
Try this regex:
^subdomain.domain.co.uk\/((?!folder).)*$
Demo here:
Regex101
First off, you need slashes around "folder", otherwise you'll also exclude "/anotherfolder/" and "/folder.jpg" etc.
Put the negative look ahead before the "." and add "." before "folder":
subdomain.domain.co.uk\/(?!.*\/folder\/).*
This won't match a URL with "/folder/" anywhere in it.

RegEx | Match string not starting with a specific word

I am writing some mod_rewrite regex, and I have several request URLs that lookg like that
use1mycompany
use2mycompany
use3mycompany
use4mycompany
but also I have and some request URIs that start with
mycompany/user1mycompany/
mycompany/user2mycompany/
mycompany/user3mycompany/
mycompany/user4mycompany/
In order to fix that Issue I have used the following regex in apachec mod_rewrite
^(.*)mycompany/?$
the problem is that my regex matcing both the userXmycomany URL and the mycopmany/userXmycompany/
So, the question is, how can I match the urls that not starts with the string "mycompany" but end with the string "mycompany" ?
Kind regards
(?<!mycompany)/.*(?<=mycompany)/?
will match /user1mycompany/ but not mycompany//user1mycompany/
You should match string which have at least one character before mycompany.
* means 0 or more. + means 1 or more.
^(.+)mycompany/?$

Regex match url without file extension

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 '/'.

RegEx to exclude a string

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].*)))))