How remove escape characters - c#

In the page script I have:
imgLocStr[0][0] = "gau"
imgLocStr[1][0] = "swo"
imgLocStr[2][0] = "gau"
imgLocStr[0][1] = "swo"
imgLocStr[1][1] = "swo"
imgLocStr[2][1] = "swo"
imgLocStr[0][2] = "swo"
imgLocStr[1][2] = "swo"
imgLocStr[2][2] = "swo"
But when Im parsing the html code I got"
"imgLocStr[0][0] = \"gau\"\n "
How I remove the \ and and \n at the end only need the values "gau" or "swo"
I can use IndexOf("\") and SubString() but Im sure there is an easy way to do this.

You can use Regex.Replace(originalString, #patern, string_to_sustitute)which returns a new string with all the apperances of the pattern replaced with the string_to_sustitute

Try Regex: "\w+" to get "gau" or "sau".
Demo
use Regex.Match in C#

The backslashes \" are to escape the double quote in the string.
To remove the newline and the space at the end you might use \s+$ to match 1+ times a whitespace character till the end of the string.
Fo example:
String str = "imgLocStr[0][0] = \"gau\"\n ";
Console.WriteLine(Regex.Replace(str, #"\s+$", "")); // imgLocStr[0][0] = "gau"
To get your value using a regex you might use an alternation and specify your allowed values:
"(?:gau|swo)"\s*$
demo
Or use a negated character class to match not a double quote or a newline:
"[^"\r\n]+"\s*$
demo
You could specify a more exact pattern using an anchor to assert the string of the string ^ and specify what you would allow between the opening and the closing quotes at the end of the string $:
^imgLocStr\[\d+\]\[\d+\] = ("\w+")\s*$
demo

Related

How do I remove specific character before and after single quote using regex

I have a text string with single quotes, I'd like to remove the parenthesis before and after that single quotes by using regular expression. Could anyone suggest me Thank you.
For example,
I have (name equal '('John')') the result that I expect is name equal '('John')'
// Using Regex
string input = "(name equal '('John')')";
Regex rx = new Regex(#"^\((.*?)\)$");
Console.WriteLine(rx.Match(input).Groups[1].Value);
// Using Substring method
String input= "(name equal '('John')')";
var result = input.Substring (1, input.Length-2);
Console.WriteLine(result);
Result:
name equal '('John')'
Try this:
var replaced = Regex.Replace("(name equal '('John')')", #"\((.+?'\)')\)", "${1}");
The Regex class is in the System.Text.RegularExpressions namespace.
Use negative look behind (?<! ) and negative look ahead (?! ) which will stop a match if it encounters the ', such as
(?<!')\(|\)(?!')
The example explains it as a comment:
string pattern =
#"
(?<!')\( # Match an open paren that does not have a tick behind it
| # or
\)(?!') # Match a closed paren tha does not have tick after it
";
var text = "(name equal '('John')')";
// Ignore Pattern whitespace allows us to comment the pattern ONLY, does not affect processing.
var final = Regex.Replace(text, pattern, string.Empty, RegexOptions.IgnorePatternWhitespace);
Result
name equal '('John')'

How to replace the single slash to double slash in C#?

Replce single slash to double slash is not working always return the single slash..
string input;
input = "\r\t";
string mat1= input.Replace("\\\\","\\\\\\\\");
string inputt= mat1;
if i am run the above code it will return output is \r\t only....
but i need output like this
\r\t
"\r\t" is in fact just two characters, carriage return and tab. This is because the \ escape character is used to specify special characters.
If you want to have a string that is actually "\r\t" you need to escape the \ characters by using \\.
So your string should be:
input = "\\r\\t";
Or
input = #"\r\t";
And then to replace the backslashes with double backslashes:
string mat1= input.Replace("\\","\\\\");
Or
string mat1= input.Replace(#"\", #"\\");
input = "\r\t";
is a so called escaped string. \ means escape sequence. if you need \r\t you need to write
input = "\\r\\t";
This is 2 already escaped characters, not 4.
input="\r\t";
\r and \t are special literals. Check this article:
\r - Carriage return
\t - Horizontal tab
What you wanted, I gess, is to change this special literal like this:
string input;
input = "\r\t";
input = input.Replace("\r", "\\r");
input = input.Replace("\t", "\\t");
Console.WriteLine(input);

Regex pattern for text between 2 strings

I am trying to extract all of the text (shown as xxxx) in the follow pattern:
Session["xxxx"]
using c#
This may be Request.Querystring["xxxx"] so I am trying to build the expression dynamically. When I do so, I get all sorts of problems about unescaped charecters or no matches :(
an example might be:
string patternstart = "Session[";
string patternend = "]";
string regexexpr = #"\\" + patternstart + #"(.*?)\\" + patternend ;
string sText = "Text to be searched containing Session[\"xxxx\"] the result would be xxxx";
MatchCollection matches = Regex.Matches(sText, #regexexpr);
Can anyone help with this as I am stumped (as I always seem to be with RegEx :) )
With some little modifications to your code.
string patternstart = Regex.Escape("Session[");
string patternend = Regex.Escape("]");
string regexexpr = patternstart + #"(.*?)" + patternend;
The pattern you construct in your example looks something like this:
\\Session[(.*?)\\]
There are a couple of problems with this. First it assumes the string starts with a literal backslash, second, it wraps the entire (.*?) in a character class, that means it will match any single open parenthesis, period, asterisk, question mark, close parenthesis or backslash. You'd need to escape the the brackets in your pattern, if you want to match a literal [.
You could use a pattern like this:
Session\[(.*?)]
For example:
string regexexpr = #"Session\[(.*?)]";
string sText = "Text to be searched containing Session[\"xxxx\"] the result would be xxxx";
MatchCollection matches = Regex.Matches(sText, #regexexpr);
Console.WriteLine(matches[0].Groups[1].Value); // "xxxx"
The characters [ and ] have a special meaning with regular expressions - they define a group where one of the contained characters must match. To work around this, simply 'escape' them with a leading \ character:
string patternstart = "Session\[";
string patternend = "\]";
An example "final string" could then be:
Session\["(.*)"\]
However, you could easily write your RegEx to handle Session, Querystring, etc automatically if you require (without also matching every other array you throw at it), and avoid having to build up the string in the first place:
(Querystring|Session|Form)\["(.*)"\]
and then take the second match.

Need Regex to match [#URL^Url Description^#]

I need regex to find this text
[#URL^Url Description^#]
in a string and replace it with
Url Description
"Url Description" can be set of characters in any language.
Any Regex Experts out there to help me?
Thanks.
It might be a bit confusing, but you can use the following:
string str = #"[#URL^Url Description^#]";
var regex = new Regex(#"^[^^]+\^([^^]+)\^[^^]+$");
var result = regex.Replace(str, #"$1");
The first ^ means the beginning of the string;
The [^^]+ means anything not a caret character;
The \^ is a literal caret;
The $ is the end of the string.
Basically, it captures all characters between the carets (^) and replace this in between the <a> tags.
See ideone demo.
You can also replace the last line with this:
var result = regex.Replace(str, #"$1");
Where link is the variable containing the link you want to replace in.
Why don't you use String.Replace()? A regex would work, but it looks like the format is well defined and regexes are harder to read.
string url = "[#URL^blah^#]";
string url_html = url.Replace("[#URL^", "<a href=\"http://www.somewhere.net\">")
.Replace("^#]", "</a>");

C# Regex search string for text including surrounding brackets

I would like to search a string for '[E1010]' or '[E1011]' or '[E1012]'. Currently, I can only successfully search without using the brackets []. How can I adjust my regex to include the texting surrounded by the brackets as it is in my sClientError variable.
Thanks!
string sClientErrors = "Bla Blah \"30\" [E1011]\r\nBlah Blah"44\" [E1012]";
Regex myRegexE10 = new Regex(#"\bE1010\b");
Regex myRegexE11 = new Regex(#"\bE1011\b");
Regex myRegexE12 = new Regex(#"\bE1012\b");
if (myRegexE10.IsMatch(sClientErrors) || myRegexE11.IsMatch(sClientErrors) || myRegexE12.IsMatch(sClientErrors))
{
// do code here...
}
By adding the brackets:
Regex myRegexE10 = new Regex(#"\[E1010]");
or
Regex myRegexE1x = new Regex(#"\[E101[012]]");
if (myRegexE1x.IsMatch(sClientErrors)) { ...
Note that once you add the brackets, word boundaries are no longer necessary. Note too that you don't need to escape closing square brackets
You can put a "\" if front of a character you want to include, so you would use:
Regex myRegexE10 = new Regex(#"\[\bE1010\b\]")
You can also use "\\" if you needed to find something like "\s", where "\*" is a Regex option.

Categories

Resources