Nginx Regex example to get a value between two commas - regex

Suppose I am hitting the URL
http://www.example.com?state=env,test,appname,maha,observation_code,123456
and in $args_state I am getting this string
state=env,test,appname,maha,observation_code,123456
I want value between first and second comma
In this example if we perform regex then it should return test. based on that value I want to redirect url to somewhere else.
I am making some changes in nginx.conf file.

Unless other conditions are needed, simple string functions will more than suffice:
t = """state=env,test,appname,maha,observation_code,123456""".split("=")[1].split(',')[1]
print(t)
# test

Related

Regular expressions (RegEx) to filter string from URLs in Google Analytics

I want to filter a string from the URLs in Google Analytics. This can be done using the Views > Filter > Exclude using RegEx, but I have been unable to get it to work.
An outline of how these filters are set up, can be found here, however, I can not work out how to isolate the string using RegEx. I believe it will need to be one filter per URL type.
The URLs follow this format:
/software/11F372288FA/pagename
/software/13F412C5FA/pagename/summary
/software/XIL1P0BFXCKM81/pagename2
I need to exclude this part of the URL:
/11F372288FA/
So that the URL data (e.g. Session time) is recorded against:
/software/pagename
/software/pagename/summary
/software/pagename2
I have worked out that I can isolate the string using thing following RegEx
^\/validate\/(..........)\/accounts\/summary$
It is not very elegant and would require a filter for every URL type.
Thanks for the help!
I'm not certain if this will work in your exact case but instead of using regex for this it might be easier to just create a new string from the start to the end of "software" and append everything from pagename to the end. In Java this might look something like:
String newString = oldString.substring(0, 9) + oldString.substring(oldString.indexOf("pagename"));
Take note though that this will only work if the "software" at the start is always the same length and you are actually only excluding things between "software" and "pagename".

How to match 2 query strings from one URL for Wordpress Redirection Plugin

I want to use the 'Redirection' plugin for WordPress to redirect search URLS to a cleaner URL.
The Redirection plugin's source URL accepts regular expressions.
ONE EXAMPLE OF WHAT I NEED:
I need this search URL:
http://www.danceclass.es/?s=&where=Sydney+%28Australia%29&company_category=pole&cat=pole&search_simple=STEP+3+-+Show+details
to redirect to:
http://www.danceclass.es/pole-sydney
but only if where=Sydney AND cat=pole.
Can a Regular Expressions expert please translate this example into regex code for me?
I'll rinse and repeat for different where= AND cat=
Since you need to extract the values of those 2 query parameters, you'll need 2 capturing groups. However the regex also needs to handle different query parameter order i.e cat and where order can be different
Here is the regex
https://regex101.com/r/gP9pZ0/1
You can use captured groups to form your new url, an example using sed's substitute(s) command:
$sed -r 's#^.*where=([a-zA-Z]*).*cat=([^&]*).*|.*cat=([^&]*).*where=([a-zA-Z]*).*$#http://www\.danceclass\.es/\2\3-\1\4#'
http://www.danceclass.es/?s=&where=Sydney+%28Australia%29&company_category=pole&cat=pole&search_simple=STEP+3+-+Show+details
http://www.danceclass.es/pole-Sydney
http://www.danceclass.es/?s=&company_category=pole&cat=pole&search_simple=STEP+3+-+Show+details&where=Sydney+%28Australia%29
http://www.danceclass.es/pole-Sydney
$
Once you the your final URL, you can make the string lowercase which meets your answer. The 2 inputs given to sed have different order i.e 1st URL has where then cat, order is reverse for 2nd URL
No need to iterate over different values, you can match as per the valid chars mentioned in regex, i.e.:
cat=[^&] cat value will give you every char until it doesn't find &
where=[a-zA-Z] where value will give you every char if it is an uppercase/lowercase char

Regex for BBCode with optional parameters

I'm currently stuck on a regex. I'm trying to fetch the contents of a BBCode, that has optional params and maybe different notations:
[tag]https://example.com/1[/tag]
[tag='https://example.com/2'][/tag]
[tag="http://another-example.com/whatever"][/tag]
[tag=ftp://an-ftp-host][/tag]
[tag='https://example.com/3',left][/tag]
[tag="https://example.com/4",right][/tag]
[tag=https://example.com/5][/tag]
[tag=https://example.com/i-need-this-one,right]http://example.com/i-dont-need-this-one[/tag]
The 2nd param can just be left or right and if this is given, i need the URL from the first param. Otherwise, i need that one between the tags.
An url as param can be wrapped within ' or " or without any of these.
My current regular expression is this:
~\[tag(?|=[\'"]?+([^]"\']++)[\'"]?+]([^[]++)|](([^[]++)))\[/tag]~i
However, this one also includes the 2nd param in the match list and a lot more of things, that i don't want to match.
Any suggestions?
I've made some changes to do what you want. I've included your version here for easy comparison:
Yours: http://regex101.com/r/dE4aE4/1
\[tag(?:=[\'"]?(.*)[\'"]?)?]([^]]*)?\[/tag]
Mine: http://regex101.com/r/dE4aE4/3
\[tag(?:=[\'"]?([^,]*?)(?:,[^]'"]+)?[\'"]?)?]([^\[]+)?\[/tag]
Observe that I've changed a bit to get the URL without the coma (,): from (.*) to ([^,]*?)(?:,[^]'"]+)?
I've also fixed the content part: from ([^]]*)? to ([^\[]+)?

Extract last part of url without query string or jsessionid

I want a regex that will always return the last part of an url before the query string parameters and without the jessionid if present.
Here's some url examples:
http://www.somesite.com/some/path/test.action;jsessionid=000063vCmvJAn7VWyymA_dPsHZs:16u9pglit?sort=2&param1=1&param2=2
http://www.somesite.com/some/path/test;jsessionid=000063vCmvJAn7VWyymA_dPsHZs:16u9pglit?sort=2&param1=1&param2=2
http://www.somesite.com/some/path/test.action?sort=2&param1=1&param2=2
http://www.somesite.com/some/path/test?sort=2&param1=1&param2=2
Here's my regex so far:
.*http://.*/some/path.*/(.*);?.*\?.*
It is working for the url that does not contain jsessionid, but will return test;jessionid=... if it is present.
To test: http://regex101.com/r/fM0mE2
I would use this regex:
.*http:\/\/.*\/some\/path.*\/([^;\?]+);?.*\?.*
^^^^^^
Basically matches anything that isn't ; or ?. And I think it might be shortened to:
.*http:\/\/.*\/some\/path.*\/([^;\?]+)

How do I match the question mark character in a Django URL?

In my Django application, I have a URL I would like to match which looks a little like this:
/mydjangoapp/?parameter1=hello&parameter2=world
The problem here is the '?' character being a reserved regex character.
I have tried a number of ways to match this... This was my first attempt:
(r'^pbanalytics/log/\?parameter1=(?P<parameter1>[\w0-9-]+)&parameter2=(?P<parameter2>[\w0-9-]+), 'mydjangoapp.myFunction')
This was my second attempt:
(r'^pbanalytics/log/\\?parameter1=(?P<parameter1>[\w0-9-]+)&parameter2=(?P<parameter2>[\w0-9-]+), 'mydjangoapp.myFunction')
but still no luck!
Does anyone know how I might match a '?' exactly in a Django URL?
Don't. You shouldn't match query string with URL Dispatcher.
You can access all values using request.GET dictionary.
urls
(r'^pbanalytics/log/$', 'mydjangoapp.myFunction')
function
def myFunction(request)
param1 = request.GET.get('param1')
Django's URL patterns only match the path component of a URL. You're trying to match on the querystring as well, this is why you're having trouble. Your first regex does what you wanted, except that you should only ever be matching the path component.
In your view you can access the querystring via request.GET
The ? character is a reserved symbol in regex, yes. Your first attempt looks like proper escaping of it.
However, ? in a URL is also the end of the path and the beginning of the query part (like this: protocol://host/path/?query#hash.
Django's URL dispatcher doesn't let you dispatch URLs based on the query part, AFAIK.
My suggestion would be writing a django view that does the dispatching based on the request.GET parameter to your view function.
The way to do what the original question was i.e. catch-all in URL dispatch var...
url(r'^mens/(?P<pl_slug>.+)/$', 'main.views.mens',),
or
url(r'^mens/(?P<pl_slug>\?+)/$', 'main.views.mens',),
As far as why this is needed, GET URL's don't exactly provide good "permalinks" or good presentation in general for customers and to clients.
Clients often times request the url be formatted i.e.
www.example-clothing-site.com/mens/tops/shirts/t-shirts/Big_Brown_Shirt3XL
this is a far more readable interface for the end-user and provides a better overall presentation for the client.