Perl/lighttpd regex - regex

I'm using regex in lighttpd to rewrite URLs, but I can't write an expression that does what I want (which I thought was pretty basic, apparently not, I'm probably missing something).
Say I have this URL: /page/variable_to_pass/ OR /page/variable_to_pass/
I want to rewrite the URL to this: /page.php?var=variable_to_pass
I've already got rules like ^/login/(.*?)$ to handle specific pages, but I wanted to make one that can match any page without needing one expression per page.
I tried this: ^/([^.?]*) but it matches the whole /page/variable_to_pass/ instead of just page.
Any help is appreciated, thanks!

This regexp should do what you need
/([^\/]+)/(.+)
First match would be page name, and the second - variable value

Try:
/([^.?])+/([^.?])+/
That should give you two matches.

Related

REGEX: find URL with specific words/pages

I have the current regex exp:
http[s]?://(?:[a-zA-Z]|[0-9]|[$-_#.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+
Which retrieves all the urls from a file, but I need it to only get the urls with a specific page, let's say page-to-find and I can't seem to do it without having the expression to add to a second group and I only want it natively in one group instead of two, as direct as possible.
Any tips?
Thanks
If its a page what does it end in? .asp? .php? .aspx? .htm? .html? (Something else?)
Try this for a start:
http[s]?://.*page-to-find

Regex URL in django

Maybe this question is repeat it but I cant find an appropriate answer for my specific issue. I have two URL's:
url(r'^dashboard/completar-perfil/(?P<pk>[-_\w]+)/$', CompleteProfileView.as_view()),
url(r'^dashboard/.*$', DashboardView.as_view()),
As you can see both begin with dashboard. Problem is the first one does not render CompleteProfileView, always renders DashboardView, if I remove dashboard/ from the first URL, it does work fine, how can I achieve that both urls render each of their respective views?
The problem is that ^dashboard/.*$ is a greedy regular expression that will match everything that start with dashbord/, including dashboard/completar-perfil/.
So, you may need specify better the second regex. Do you really need .* ?
If it is the index of your dashboard, you could use ^dashboard/$. Otherwise, you could put another word between dashboard and your greedy regex, like the following:
r"^dashboard/another-word/.*$"

Regex, optional match in url

I spend a couple of hour with no good result (maybe my mood is not helping about it).
I am trying to build a regex to help me match both urls:
/reservables/imagenes/4/editar/6
/reservables/imagenes/4/subir
As you note above, the last segment in the first url 6 is not present at the end of the second url, because this segments is optional here. So I need to match both urls in one regex, for that, I have tried this:
reservables/(editar|imagenes)/([0-9]+)/(imagen|editar|actualizar|subir)/([0-9]+)
That works fine only for the first url. So, reading a few notes about regex it suggest me that I need the ? symbol, right? So, I tried this one, but it did not work:
reservables/(editar|imagenes)/([0-9]+)/(imagen|editar|actualizar|subir)/([0-9]+)?
Well, I do not what I am doing wrong.
You want to put the ? around the / as well, like so:
reservables/(editar|imagenes)/([0-9]+)/(imagen|editar|actualizar|subir)(?:/([0-9]+))?
You can see that it matches correctly on debuggex.
This one will work:
reservables/(editar|imagenes)/([0-9]+)/(imagen|editar|actualizar|subir)/([0-9]*)

Regex mid-string pattern exclustion

I need help with a regular expression for use with UrlRewriting.Net. I have two URLs -
http://domain/SomeFolder/tracks/SomeFileName/
and
http://domain/SomeFolder/<could be anything>/SomeFileName/
For URL rewriting purposes I need to come up with one expression that will let me target specifically the URL with "tracks" in the middle of it. I need another expression to catch everything without "tracks" in it.
Before I had this constraint I was using ^~/SomeFolder/([^/]*)/SomeFileName/?$ and that worked as my catch-all. Now that I have this specific "tracks" folder, I can't use the catch all.
Make sense?
Many thanks for the help!
^~/SomeFolder/(?!tracks/)([^/]*)/SomeFileName/?$
and
^~/SomeFolder/(tracks)/SomeFileName/?$

Regex to find bad URLs in a database field

We had an issue with the text editor on our website that was doubling up the URL. So for example, the text field may look contain:
This is a description for a media item, and here in a link.
So pretty much I need a regex to detect any string that begins with http and has another http before a closing quote, as in "http://www.example.com/apage.htmlhttp://www.example.com/apage.html"
"http[^"]+http
http://www.example.com/apage.htmlhttp://www.example.com/apage.html
This is actually a valid URL! So you'd want to be a bit careful not to munge any other URLs that happen to have ‘http://’ in the middle of them. To detect only a ‘doubled’ URL you could use backreferences:
"(https?://[^"]*)\1"
(This is a non-standard regex feature, but most modern implementations have it.)
Using regex to process HTML is a bad idea. HTML cannot reliably be parsed by regex.
If you can use the *.? syntax, you can just look for the following:
http(.*?)http
and if its present, reject the url.
The string that begins with http and has another http before a quote is:
^http[^"]*http
But, although this answers exactly your question I suspect you may want Uh Clem's answer instead ;-)
You will probably want something like this:
("http[^"]+)(http)
Then compare the two and if \1 === " + \2 then replace them.
One thought; do you have any query strings in any of your urls. If you do, are any of them like this "http://someurl.com?http=somemoredatahttp://someurl.com?http=somemoredata"?
If so, you will want something far more complicated.