I am trying to deal with a url string
http://localhost:8000/lastnames/location/city/215722?filter=beginswith:p&paging=(offset:2,limit:2)
How do I handle parsing out those sub objects? The (offset:2,limit:2) just gets parsed out as a string. These are accepted delimiters in the URL spec so I thought something like url.parse (in node) would handle this.
The "URL specification" (actually, "Uniform Resource Identifier (URI): Generic Syntax, RFC-3986" defines the syntax of the query component to be:
query = *( pchar / "/" / "?" )
pchar = unreserved / pct-encoded / sub-delims / ":" / "#"
In other words, sub-delims (which include parentheses) and colons really are just ordinary characters in a query.
If requested (by passing true as its second argument), url.parse will also split the query into key-value assignments using the sub-delims = and &, as per the application/x-www-form-urlencoded format. Other sub-delims are not involved in query string encoding.
Note that url.parse doesn't decode pct-encoded sequences such as %25; for that, you need decodeURIComponent. That should be done only after the components are fully broken down into their parts.
In short, if you want to parse (offset:2,limit:2) into some other structured object, you'll need to do that yourself, possibly by using regexes or -- if the format is complicated enough -- a parser generator like jison. In any event, you should leave the percent-decoding step until the very end; otherwise, percent-encoded sub-delims won't be parsed correctly.
Related
I have a regular expression that I use several times in a script, where a single word gets changed but the rest of the expression remains the same. Normally I handle this by just creating a regular expression string with a format like the following example:
# Simple regex looking for exact string match
$regexTemplate = '^{0}$'
# Later on...
$someString = 'hello'
$someString -match ( $regexTemplate -f 'hello' ) # ==> True
However, I've written a more complex expression where I need to insert a variable into the expression template and... well regex syntax and string formatting syntax begin to clash:
$regexTemplate = '(?<=^\w{2}-){0}(?=-\d$)'
$awsRegion = 'us-east-1'
$subRegion = 'east'
$awsRegion -match ( $regexTemplate -f $subRegion ) # ==> Error
Which results in the following error:
InvalidOperation: Error formatting a string: Index (zero based) must be greater than or equal to zero and less than the size of the argument list.
I know what the issue is, it's seeing one of my expression quantifiers as a replacement token. Rather than opt for a string-interpolation approach or replace {0} myself, is there a way I can tell PowerShell/.NET to only replace the 0-indexed token? Or is there another way to achieve the desired output using format strings?
If a string template includes { and/or } characters, you need to double these so they do not interfere with the numbered placeholders.
Try
$regexTemplate = '(?<=^\w{{2}}-){0}(?=-\d$)'
I have a query that looks like this:
INSERT INTO table VALUES ('47677;2019;2019;10T-1001-10010AS;A05;International;TieLineKoman-KosovoB;L_KOM-KOSB;2018;NULL;NULL;;NULL;Tieline;NULL;10XAL-KESH-----J;0;3')
that is produced by parsing a csv file.
The query is not in a valid form, I have to replace all semicolons with the string ',' (comma inside single quotes). What I want to get is:
('47677','2019','2019','10T-1001-10010AS','A05','International','TieLineKoman-KosovoB','L_KOM-KOSB','2018','NULL','NULL','','NULL','Tieline','NULL','10XAL-KESH-----J','0','3')
I have tried to do this in many different ways, but I end up with backshlashes added in my string. This is what I get:
"INSERT INTO AllocatedEICDetail VALUES ('47677\\',\\'2019\\',\\'2019\\',\\'10T-1001-10010AS\\',\\'A05\\',\\'International\\',\\'TieLineKoman-KosovoB\\',\\'L_KOM-KOSB\\',\\'2018\\',\\'NULL\\',\\'NULL\\',\\'\\',\\'NULL\\',\\'Tieline\\',\\'NULL\\',\\'10XAL-KESH-----J\\',\\'0\\',\\'3')"
Any ideas how to do this properly without having the backslashes added?
Thank you!
//the string you have
const string = '47677;2019;2019;10T-1001-10010AS;A05;International;TieLineKoman-KosovoB;L_KOM-KOSB;2018;NULL;NULL;;NULL;Tieline;NULL;10XAL-KESH-----J;0;3';
//the string you need:
const targetString = string.replace(/\;/g,',');
You specify a small regex between the forward slashes in replace which is a simple ';', give it a 'g' flag for global which will replace all instances, and in the second argument supply what you need it replaced with.
Spaces are useful to indent urls, sql queries to make it more readable.
Is there a way to remove characters from a const string at compile time in golang ?
ex: (runtime version)
const url = `https://example.com/path?
attr1=test
&attr2=test
`
// this is the code to be replaced
urlTrim := strings.Replace(
strings.Replace(url, "\n", "", -1)
)
Constant expressions cannot contain function calls (except a few built-in functions). So what you want cannot be done using a raw string literal.
If your goal with using multiple lines is just for readability, simply use multiple literals and concatenate them:
const url = "https://example.com/path?" +
"attr1=test" +
"&attr2=test"
Try it on the Go Playground.
See related question: Initialize const variable
I have a string to modify as per the requirements.
For example:
The given string is:
str1 varchar = '123,456,789';
I want to show the string as:
'456,789'
Note: The first part (delimited) with comma, I want to remove from string and show the rest of string.
In SQL Server I used STUFF() function.
SELECT STUFF('123,456,789',1,4,'');
Result:
456,789
Question: Is there any string function in PostgreSQL 9.3 version to do the same job?
you can use regular expressions:
select substring('123,456,789' from ',(.*)$');
The comma matches the first comma found in the string. The part inside the brackets (.*) is returned from the function. The symbol $ means the end of the string.
A alternative solution without regular expressions:
select str, substring(str from position(',' in str)+1 for length(str)) from
(select '123,456,789'::text as str) as foo;
You could first turn the string to array and return second and third cell:
select array_to_string((regexp_split_to_array('123,456,789', ','))[2:3], ',')
Or you could use substring-function with regular expressions (pattern matching):
SELECT substring('123,456,789' from '[0-9]+,([0-9]+,[0-9]+)')
[0-9]+ means one or more digits
parentheses tell to return that part from the string
Both solutions work on your specific string.
Your The SQL Server example indicates you just want to remove the first 4 characters, which makes the rest of your question seem misleading because it completely ignores what's in the string. Only the positions matters.
Be that as it may, the simple and cheap way to cut off leading characters is with right():
SELECT right('123,456,789', -4);
SQL Fiddle.
I am trying to convert a string in Oracle into a modified string that is compatible with a specific API.
I would like to leave all alphanumeric characters intact, replace all spaces with the + character, and replace all special characters with % plus their hex code.
For example,
Project 1: Nuts & Bolts
should become
Project+1%3A+Nuts+%26+Bolts
Is there any way to do this using only SQL?
I don't think you can get there with plain SQL without nested replace calls. You can get your sample value with the utl_url.escape() function, but because you have to pass it a second parameter and that is a boolean, you have to do it in an PL/SQL block:
set define off
begin
dbms_output.put_line(replace(utl_url.escape('Project 1: Nuts & Bolts', true),
'%20', '+'));
end;
/
Project+1%3A+Nuts+%26+Bolts
The url_utl.escape function converts the spaces to %20:
Project%201%3A%20Nuts%20%26%20Bolts
... and the single replace call converts those to +.
As Ed Gibbs said, you can make that a function so you can at least call it from plain SQL:
create or replace function my_escape(str in varchar2) return varchar2 is
begin
return replace(utl_url.escape(str, true), '%20', '+');
end;
/
set define off
select my_escape('Project 1: Nuts & Bolts') from dual;
MY_ESCAPE('PROJECT1:NUTS&BOLTS')
--------------------------------
Project+1%3A+Nuts+%26+Bolts
You probably need to check the legal and reserved character lists to see if there's anything else that needs special handling.
(I've used set define off to stop my client treating the ampersand as a substitution variable; your client or application might not need that, e.g. if calling over JDBC).
apex_util.url_encode should work.