What are the default separator for string interpolation? - julia

It seems ",", "$", "/" all serve as a separator, but "_" not.
x = "1"
"$x,x", "$x$x", "$x/1", "$x_1"
Is there any doc about this?

I believe this is because x_1 is a valid variable name in Julia, so it is trying to insert the value of that variable into the string.

The doc says:
The shortest complete expression after the $ is taken as the expression whose value is to be interpolated into the string
The internal workings are explained in the github issue #455 which could be summarised by:
The way string interpolation works is actually entirely defined in Julia. What happens is that the parser (in FemtoLisp) scans the code and finds a string literal, delimited by double quotes. If it finds no unescaped $ in the string, it just creates a string literal itself — ASCIIString or UTF8String depending on the content of the string. On the other hand, if the string has an unescaped $, it punts and hands the interpretation of the string literal to the str julia macro, which generates an expression that constructs the desired strings by concatenating string literals and interpolated values. This is a nice elegant scheme that lets the parser not worry about stuff like interpolation.
I could guess that #\, #\) #\] #\} #\; which are ,, ), ], } and ; respectively are closing tokens for expressions and $ is specifying the start of next interpolation.

Related

Malformed string from character string

I have a simple character string:
y <- "Location 433900E 387200N, Lat 53.381 Lon -1.490, 131 metres amsl"
When I perform regex capture on it:
stringr::str_extract(r'Lat(.*?)\,', y)
I get this error:
>Error: malformed raw string literal at line 1
why?
With R's raw strings (introduced in version 4.0.0), you need to use either ( or [ or { with the quotes, e.g.,
r'{Lat(.*?)\,}'
This is documented at ?Quotes (and in the release notes):
Raw character constants are also available using a syntax similar to the one used in C++: r"(...)" with ... any character sequence, except that it must not contain the closing sequence )"⁠. The delimiter pairs [] and {} can also be used, and R can be used in place of r."

In ruamel how to start a string with '*' with no quotes like any other string

yaml = ruamel.yaml.YAML()
yaml.indent(mapping=4)
test_yaml_file = open("test.yaml")
test_file = yaml.load(test_yaml_file)
# test = LiteralScalarString('*clvm')
test = "*testing"
test_file['test_perf'] = test
with open("test.yaml", 'w') as changed_file:
yaml.dump(test_file, changed_file)
In this the expected output was
test_perf: *testing
but the output has been
test_perf: '*testing'
how to achieve this using ruamel?
Your scalar starts with a *, which is used in YAML to indicate an alias node. To prevent *testing to be interpreted as an alias during loading (even though the corresponding anchor (&testing) is not specified in the document), the scalar must be quoted or represented as a literal or folded block scalar.
So there is no way to prevent the quotes from happening apart from choosing to represent the scalar as literal or folded block scalar (where you don't get the quotes, but do get the | resp. >)
You should not worry about these quotes, because after loading you'll again have the string *testing and not something that all of a sudden has extra (unwanted) quotes).
There are other characters that have special meaning in YAML (&, !, etc.) and when indicated at the beginning of a scalar cause the scalar to be quoted. What the dump routine actually does is dump the string and read it back and if that results in a different value, the dumper knows that quoting is needed. This also works with strings like 2022-01-28, which when read back result in a date, such strings get quoted automatically when dumped as well (same for strings that look like floats, integers, true/false values).

"ERROR: syntax: cannot juxtapose string literal" when ending a triple-quoted string literal with a quote

I'm trying to create a string literal representing a CSV file with quoted fields. The intended CSV looks like this:
"a","b"
"1","2"
Triple quotes work if I want a newline character at the end of the string:
julia> """
"a","b"
"1","2"
"""
"\"a\",\"b\"\n\"1\",\"2\"\n"
But if I try to make a string without the newline character at the end, then I get a syntax error:
julia> """
"a","b"
"1","2""""
ERROR: syntax: cannot juxtapose string literal
Is there a simple way to get around this?
As an aside, note that there is no syntax error when you start the string-literal with a quote:
julia> """"a","b"
"1","2"
"""
"\"a\",\"b\"\n\"1\",\"2\"\n"
The issue is that this by itself is a valid string literal:
"""
"a","b"
"1","2"""
When you follow that with another " the parser thinks "woah, you can’t just follow a string with another string". You can force it to not consider the quote after 2 as part of a closing """ sequence by escaping it with \:
"""
"a","b"
"1","2\""""
At the start of the string, there's no such issue since the first three " characters are taken to start a string literal and the following " must just be a quote character inside of the string, which is what you want.
I'm not sure what you would consider the best solution to be. The options are:
Escape the " at the end;
Put the closing """ on a separate line.
The latter seems better to me, but it's your call.
See the Julia Docs for other examples on Triple-Quoted String Literals.

Expression for finding an index in an array

How can I find the first character in a string that is the space character and return its index, with a single expression that can be used as part of Contract_Cases?
For example, if the string is:
Input : constant String := "abc def";
then the expression should return 4.
The question originally asked for the first non-blank character in the string, for which you need Ada.Strings.Fixed.Index_Non_Blank (ARM A.4.3(12) and (61)).
As amended (the first blank character in the string), use Ada.Strings.Fixed.Index - see the OP’s comment below.

asp.net regex help

Hi ive got this regular expression and that extracts numbers from a string
string.Join(null,System.Text.RegularExpressions.Regex.Split(expr, "[^\\d]"));
so eg, the format of my string is like this strA:12, strB:14, strC:15
so the regex returns 121415
how can I modify the expression to return
12,14,15 instead, any suggestions please
You're calling String.Join, which joins an array of strings into a single string, separating each element by the separator parameter.
Since you're passing null as that parameter, it doesn't put anything between the strings.
You need to pass ", " instead of null to separate each string with ,.

Resources