extract string from req.url.path in VCL Fastly - varnish

I need help to extract values from string req.url.path which looks like this:
/a/b/c/d
Need to extract c, if there is url, like /a, it should return ''.
I tried
regsub(req.url.path, "/a/b/", "\5");
Tried with replace too.
but it is not effectively working for me. Please help me.

I believe the following might be along the lines of what you require:
if (req.url.path ~ "^/a/.+/c") {
set req.url = regsub(req.url.path, "/c", "") + "?" + req.url.qs;
}
It checks if the URL path begins with /a and is followed by /c (presuming also that there are segments in-between, such as /b as per your given example /a/b/c/d).
Here is a Fastly Fiddle link for you to play around with the code:
https://fiddle.fastlydemo.net/fiddle/527480ba
So given the input URL path:
/a/b/c/d?id=testing
It would change to:
/a/b/d?id=testing
Notice that the /c has been extracted from the path.

Related

search and replace a string after a match

I have a file that contains :
String url = "https://url_address/v2.0/vpc/peerings?name=change_variable"
I try to change the string change_variable with something else like
String url = "https://url_adress/v2.0/vpc/peerings?name=florian-testpeering-5"
But when I use :
sed 's/"https:\/\/url_adress\/v2.0\/vpc\/peerings?name/"https:\/\/url_adress\/v2.0\/vpc\/peerings?name=florian-testpeering-5"/'g
I Obtain :
String url = "https://url_adress/v2.0/vpc/peerings?name=florian-testpeering-5"=change_url"
What I did wrong ?
Edit :
To be more precise, I need to change the change_variable inside with a $peering who I declare before in my script.
The fact that you have forward slashes in the url, means that is better to use another character for the sed separator and so:
sed 's#String url = "https://url_address/v2.0/vpc/peerings?name=change_variable"#String url = "https://url_address/v2.0/vpc/peerings?name=florian-testpeering-5"#g'
The normal / has been changed for #, allowing for easier reading and processing of the url.
Is this what you're trying to do?
awk 'index($0,"https://url_address/v2.0/vpc/peerings?name=change_variable") { sub(/change_variable/,"florian-testpeering-5") } 1' file
String url = "https://url_address/v2.0/vpc/peerings?name=florian-testpeering-5"
If I understand your edit, and you are saying you have your string in a variable at the beginning of your script, similar to:
var='String url = "https://url_address/v2.0/vpc/peerings?name=change_variable"'
and you need to change the contents of the string replacing from name=... to the end with a new value, you can simply use bash parameter expansion with substring replacement, e.g.
var="${var/name=*/name=florian-testpeering-5\"}"
Now the variable var will contain:
String url = "https://url_address/v2.0/vpc/peerings?name=florian-testpeering-5"
You can do the same thing if the string you want to replace with is also stored in another variable, such as repl="lorian-testpeering-5", in that case your substring replacement would be:
var="${var/name=*/name=${repl}\"}"
(same result in var)

JScript Escape an ampersand in payload data for URL

I am attempting to launch a view using the following JS function:
$('#filterTop').click(function () {
var filterValue = $('#filterValueTop').val();
refreshView(`#Url.Action(Model.Action, Model.Controller)?pageSize=#Model.PageSize&pageNumber=#Model.PageNumber&sortDesc=#Model.SortDescending&filterType=#Model.FilterType&filterValue=${filterValue}&showAll=#Model.ShowAll` + `#Model.Payload`, '#Model.ResultView');
});
It worked great until I needed to append a static payload to the end of the URL. The relevant part is line 3 at the end:
&showAll=#Model.ShowAll` + `#Model.Payload`
I am assigning #Model.Payload a value of:
opts.Payload = "&batchID=" + batchID;
or "&batchID=25". The resulting URL is:
https://localhost:44303/Employee/Repaginate?pageSize=20&pageNumber=1&sortDesc=True&filterType=Name&filterValue=Jes&showAll=False&batchID=25
For some reason, it's translating the "&" to "&a.m.p;" (with no periods) which isn't a valid URL. I've tried various methods of escaping the character like using "%26", "/&", and several other garden varieties but alas, my attempts have been in vain. Any suggestions on what I am doing wrong?

Multiple lines for one String batch

I Googled it but i'm not able to find a GOOD solution.
My goal is to put a string which is composed of 6 lines in one string, and only one, in a variable.
For example, my string can look like :
a
b
c
and I want it to be in one string. I tried the thing witch ^, or with ECHO " " but it doesn't work : the cmd put an error "not recognized as an internal command" (and it's normal, it's just some sentences, not batch commands!)
Thanks, Clément
Not so simple but possible :
#echo off
REM Creating a Newline variable (the two blank lines are required!)
set NLM=^
set NL=^^^%NLM%%NLM%^%NLM%%NLM%
REM Example Usage:
setlocal enableDelayedExpansion
set "string_with_new_lines=a!nl!b!nl!c"
echo %string_with_new_lines%
Aacini has a simpler solution for this using empty variable,but I'm struggle to find the link.
Following the comments on the post answer of #npocmaka
It's currently in Python, so the """ thing works.
requete = """
PREFIX resources: <http://www.fluidops.com/resource/>
SELECT DISTINCT ?id ?marque ?modele WHERE {
?voiture resources:uid ?id.
?voiture resources:bpqmqvc ?marque. #myComment
?voiture resources:bpqmodvc ?modele
}
"""

How to find and replace a string in Matlab

So here is my problem:
I have a list of names in Matlab in a cell array.
I automatically create directories and .mat files for each name.
My problem is that some of these names contains '/' and therefore everything go wrong when I create the directory…
So I am trying to find an efficient way to find '/' and replace them.
So far I've tried to find them using the findstr function. It then gives me a cell array with the indexes where '/' appears. So when the name doesn't contain any '/' it returns {[]} and when the function find it, it returns {[i]}.
Now i'd like to have a logical condition that says if findstr is not empty then do something. I've tried with the isempty function but it doesn't work (it's never empty…)
So does anyone have a solution to this?
Thanks
Use regexprep to replace the character:
list = {'aaa', 'bb/cc', '/dd/'};
replace_from = '/'; %// character to be replaced
replace_to = '_'; %// replacing character
list_replaced = regexprep(list, replace_from, replace_to);
gives
list_replaced =
'aaa' 'bb_cc' '_dd_'

How to get the file name from the full path of the file, in vc++?

I need to get the name of a file from its full path, in vc++. How can I get this? I need only the file name. Can I use Split method to get this? If not how can I get the file name from the full path of the file?
String^ fileName = "C:\\mydir\\myfile.ext";
String^ path = "C:\\mydir\\";
String^ result;
result = Path::GetFileName( fileName );
Console::WriteLine( "GetFileName('{0}') returns '{1}'", fileName, result );
See Path::GetFileName Method
Find the last \ or /1 using one of the standard library string/char * search methods. Then extract the following text. Remember to special case where the / or \ is the last character.
1 The Windows API, for most purposes2 supports both.
1 The exception is when using long paths starting \\?\ to break 260 character limit on paths.
Directory::GetFiles Method (String, String)
Returns the names of files (including their paths) that match the specified search pattern in the specified directory.

Resources