This is my scratch:
// how to present $-$ as Kotlin string
val var1 = "\$-\$"
val var2 = "\$-$"
print("${var1.count()}: $var1")
print("${var2.count()}: $var2")
print("${var1 == var2}")
can someone explain why var2 works? (no need to escape 2nd $ char?)
A template expression in a template String in Kotlin is a dollar ($) followed by either a name or an expression in curly braces (see the String templates documentation).
The single $ sign which is followed by nothing in your String var2 does not need to be escaped because it is not a template expression.
Related
I have such a query:
WHERE x LIKE $1
, where $1 is a bindvar string built in the backend:
$1 = "%" + PATTERN + "%"
Is it possible to build a LIKE PATTERN in that way that special characters (% and _) are escaped, so I have the same functionality, but it works for all possible PATTERN values.
You would want to escape the literal % and _ with backslash. For example, in PHP we might try:
$pattern = "something _10%_ else";
$pattern = preg_replace("/([%_])/", "\\\\$1", $pattern);
echo $pattern; // something \_10\%\_ else
This code in awk doesn't work with printf :
var1 = "%-15s %-15s\n"
var2 = "\"oooo\",\"nnnn\""
var3 = var1 var2
printf var3
Updated :
no var3
printf var1,var2
And I want it works like :
printf "%-15s %-15s","oooo","nnnn"
because I'm building a visual results file of a parsing and I don't know in advance the number of arguments.
Is there any solution in this way?
I don't believe you can do the entire string like that as the commas are treated as string literals in the printf function when supplied through a single variable like that. You could do:
var1 = "\"%-15s %-15s\n\"";
var2 = "\"oooo\"";
var3 = "\"nnnn\"";
printf var1,var2,var3;
In my below script, the str_replace(rtrim(c_manager),'''','_') doesn't seem to work.
I want to replace single quotes with underscores for my arguments. For example:
Input: `S'achin`
Result: `S_achin`
$sql = 'select rtrim(f_admin_disabled),'."\n".
' convert(varchar,t_password,101),'."\n".
' rtrim(c_email),'."\n".
' str_replace(rtrim(c_manager),'''','_'),'."\n".
' rtrim(c_mgr_email)'."\n".
' from tuserprofile'."\n".
' where ic_user1 = '."'$user_id'"."\n";
If you want to have single quotes in a single quoted string to produce str_replace(rtrim(c_manager),'''','_'), you need to either escape them:
' str_replace(rtrim(c_manager),\'\'\'\',\'_\'),'
or use a different delimiter:
q! str_replace(rtrim(c_manager),'''','_'),!
to replace a character in string
$string=~s/'/_/g;
Syntax
$string=~s/<string>/<replace_string>/g;
Use this subroutine.
sub str_replace {
my ($s) = #_;
$s =~ s/'/_/g;
return $s;
}
I am trying to parse the following command from Cisco IOS config:
banner exec <d> <message> <d>
where <d> is the delimiting character of user's choice—a pound sign (#), for example. The <message> cannot use the delimiting character in it.
It seems that I will need to use semantic predicates for it. But couldn't figure out a way to do it.
Yang
As long as you know the delimiter in advance, you can use something like this. You can modify isDelimiter to support any single-character delimiter.
#lexer::members {
private boolean isDelimiter(int c) { return c == '#'; }
}
Message : Delimiter NotDelimiter* Delimiter;
fragment Delimiter : {isDelimiter(_input.LA(1))}? . ;
fragment NotDelimiter : {!isDelimiter(_input.LA(1))}? . ;
I wrote the following grammar for string variable declaration. Strings are defined like anything between single quotes, but there must be a way to add a single quote to the string value by escaping using $ letter.
grammar test;
options
{
language = Java;
}
tokens
{
VAR = 'VAR';
END_VAR = 'END_VAR';
}
var_declaration: VAR string_type_declaration END_VAR EOF;
string_type_declaration: identifier ':=' string;
identifier: ID;
string: STRING_VALUE;
STRING_VALUE: '\'' ('$\''|.)* '\'';
ID: LETTER+;
WSFULL:(' ') {$channel=HIDDEN;};
fragment LETTER: (('a'..'z') | ('A'..'Z'));
This grammar doesn't work, if you try to run this code for var_declaration rule:
VAR A :='$12.2' END_VAR
I get MismatchedTokenException.
But this code works fine for string_type_declaration rule:
A :='$12.2'
Your STRING_VALUE isn't properly tokenized. Inside the loop ( ... )*, the $ expects a single quote after it, but the string in your input, '$12.2', doesn't have a quote after $. You should make the single quote optional ('$' '\''? | .)*. But now your alternative in the loop, the ., will also match a single quote: better let it match anything other than a single quote and $:
STRING_VALUE
: '\'' ( '$' '\''? | ~('$' | '\'') )* '\''
;
resulting in the following parse tree: