Someone know how to cut a string with a SPLIT Method ?
No idea when it comes with ' \ '.
**identitiname = HttpContext.Current.User.Identity.Name;
identitiname *// it has the value FAMILY\ANDRES*
string[] usuario = identitiname.Split( '\' );**
It gives me an error code.
Regards
When we split string by \ then you have to use \\ because \ is used for formatting..
string[] usuario = identitiname.Split( '\\' );
Related
I am trying to come up a script for me substitute few special characters for texts in Python, here is a list of characters I want to replace:
: < > ? * / " | \
My code works well, if I don't add \ into the list I want to replace:
import re
subj='Test/ US: Paper* Packaging'
chars_to_remove = [':','<','>','?','*','/','"','|']
rx = '[' + re.escape(''.join(chars_to_remove)) + ']'
re.sub(rx, '', subj)
However, when I add \ into my chars_to_remove list, i will give me the error:SyntaxError: EOL while scanning string literal
import re
chars_to_remove = [':','<','>','?','*','/','"','|','\']
rx = '[' + re.escape(''.join(chars_to_remove)) + ']'
re.sub(rx, '', subj)
I know \ means add a newline in Python, but here how can I let my code knows
mean the character not newline.
Thanks
I have this grammar :
grammar Hello;
STRING : '"' ( ESC | ~[\r\n"])* '"' ;
fragment ESC : '\\"' ;
r : STRING;
I want when i type a string :
"my name is : \" StackOverflow \" "
the result will be :
"my name is : "StackOverflow" "
But this is the result when i test it :
So what should i do to fix it ? Your help will be appreciated .
There is no way to handle it in your grammar without targeting a specific language. You either strip the slashes when walking your parse tree in a listener or visitor, or embed target specific code in your grammar.
If Java is your target, you could do this:
STRING
: '"' ( ESC | ~[\r\n"] )* '"'
{
String text = getText();
text = text.substring(1, text.length() - 1);
text = text.replaceAll("\\\\(.)", "$1");
setText(text);
}
;
How do I write a lexer rule to match a String literal which does not end in an escaped quote?
Here's my grammar:
lexer grammar StringLexer;
// from The Definitive ANTLR 4 Reference
STRING: '"' (ESC|.)*? '"';
fragment ESC : '\\"' | '\\\\' ;
Here's my java block:
String s = "\"\\\""; // looks like "\"
StringLexer lexer = new StringLexer(new ANTLRInputStream(s));
Token t = lexer.nextToken();
if (t.getType() == StringLexer.STRING) {
System.out.println("Saw a String");
}
else {
System.out.println("Nope");
}
This outputs Saw a String. Should "\" really match STRING?
Edit: Both 280Z28 and Bart's solutions are great solutions, unfortunately I can only accept one.
For properly formed input, the lexer will match the text you expect. However, the use of the non-greedy operator will not prevent it from matching something with the following form:
'"' .*? '"'
To ensure strings are tokens in the most "sane" way possible, I recommended using the following rules.
StringLiteral
: UnterminatedStringLiteral '"'
;
UnterminatedStringLiteral
: '"' (~["\\\r\n] | '\\' (. | EOF))*
;
If your language allows string literals to span across multiple lines, you would likely need to modify UnterminatedStringLiteral to allow matching end-of-line characters.
If you do not include the UnterminatedStringLiteral rule, the lexer will handle unterminated strings by simply ignoring the opening " character of the string and proceeding to tokenize the content of the string.
Yes, "\" is matched by the STRING rule:
STRING: '"' (ESC|.)*? '"';
^ ^ ^
| | |
// matches: " \ "
If you don't want the . to match the backslash (and quote), do something like this:
STRING: '"' ( ESC | ~[\\"] )* '"';
And if your string can't be spread over multiple lines, do:
STRING: '"' ( ESC | ~[\\"\r\n] )* '"';
When i am trying to read data from the excel file it giving me error as "Invalid escape sequence (valid ones are \b \t \n \f \r \" \' \ )"
How to resolve this??
My string is
String path = "C:\Documents and Settings\Desktop\Param.xlsx";
I have tried both the way but couldn't able to run my script.
I 'm writing java program to read data from the excel file. so for path i used that string but unable to make it happen!
Please help me.
Try this one:
int i=0;
StringTokenizer strToken = new StringTokenizer(path, "\\");
while (strToken.hasMoreTokens()) {
i += 1;
System.out.println(i + ". element: " + strToken.nextToken());
}
You have not provided so much information so I am trying to guess what you are actually doing. But assuming that you want to open an Excel file in C# then your string is wrong because it contains invalid escape sequences. However, that is easily fixed:
String path = #"C:\Documents and Settings\Desktop\Param.xlsx";
Notice the # in front of the string.
Or you could escape the backslashes in the string:
String path = "C:\\Documents and Settings\\Desktop\\Param.xlsx";
I have the following deffinition of fragment:
fragment CHAR :'a'..'z'|'A'..'Z'|'\n'|'\t'|'\\'|EOF;
Now I have to define a lexer rule for string. I did the following :
STRING : '"'(CHAR)*'"'
However in string I want to match all of my characters except the new line '\n'. Any ideas how I can achieve that?
You'll also need to exclude " besides line breaks. Try this:
STRING : '"' ~('\r' | '\n' | '"')* '"' ;
The ~ negates char-sets.
ut I want to negate only the new line from my CHAR set
No other way than this AFAIK:
STRING : '"' CHAR_NO_NL* '"' ;
fragment CHAR_NO_NL : 'a'..'z'|'A'..'Z'|'\t'|'\\'|EOF;