This question already has answers here:
What characters need to be escaped in .NET Regex?
(4 answers)
Closed 7 years ago.
I use dynamically built regex. Problem is when symbol = "aaaa (1)" because regex tries to parse it, but I want to treat it literary
Regex regex = new Regex(#"(^" + "/(" + symbol + #" \(\d+\)$)|" + symbol);
You need to escape special chars:
var escapedSymbol = Regex.Escape(symbol);
Regex regex = new Regex(#"(^" + "/(" + escapedSymbol + #" \(\d+\)$)|" + escapedSymbol );
Reffer: msdn
Related
This question already has answers here:
What special characters must be escaped in regular expressions?
(13 answers)
Closed 4 years ago.
I'm trying to instantiate a new Regex class with the regular expression
var reg = new Regex("[[" + token.Key + "]]");
But I'm getting the following error
System.ArgumentException: 'parsing "[[Impact]]" - [x-y] range in reverse order.'
How can I instantiate the new class with the double square brackets in the regular expression?
You could use built-in functions to escape things:
var re = Regex.Escape("[[") + token.Key + Regex.Escape("]]");
var reg = new Regex(re);
But you may also want to escape the token.Key?
Or just use a string.Contains(...), if you don't want the special features of regex.
You need to escape the characters "[" and "]" for the regular expression, and if you are using "\" to escape the brackets, then you need to escape those in your C# string as well.
That means it can be either:
"\\[\\[" + key + "]]"
or
#"\[\[" + key + "]]"
or
Regex.Escape("[[") + key + "]]"
You don't need to escape the latter "]" because Regex recognizes them as normal characters if you don't have an unescaped "[" before them. I tried it on Regexpal (a regex testing website) and other answers suggest it as well.
This one works:
var reg = new Regex(#"\[\[" + token.Key + #"]]");
(Note: the second # is not actually needed)
Update:
This is even better:
var reg = new Regex($#"\[\[{token.Key}]]");
Just escape every bracket with a bracket like is shown below:
var reg = new Regex("[[[["+ token.Key +"]]]]");
Try
var reg = new Regex("[[][[]" + token.Key + "[]][]]");
it will match, for example [[Impact]] if token.Key equals "Impact".
This question already has answers here:
Why \b does not match word using .net regex
(2 answers)
Closed 5 years ago.
I want to replace certain words in one sentence string with regex replace.
For it, I create pattern array :
string[] words = {"abc","132","qwe","bold","test"};
and for replace, I do it :
foreach (string item in words){
output = Regex.Replace(output,#"\b" + item + "\b", " ");
}
but this way don't work ...
Someone has an idea?
Explanation
I use the above method in VB.net and will respond without problems.
I am a beginner in C #
It looks like you forgot the # literal at the second \b:
either put it in there
output = Regex.Replace(output, #"\b" + item + #"\b", " ");
or double the backslash so it is used as an escape character:
output = Regex.Replace(output, #"\b" + item + "\\b", " ");
This question already has answers here:
Escape double quotes in a string
(9 answers)
Closed 8 years ago.
I have a problem, because everytime i need to write indexof, i need to use +
for example:
String lsdfrom2 = '[' + '"' + "LSD" + ',' + '[' + ']' + ',' + '{' + '"' + "token" + '"' + ":" + '"';
How to write it in another way? thats soo anoying to write it like in example
I mean, how to read text below without write every char singly
["LSD",[],{"token":"
Write it as a string (escaping the quotes is required via \")
String lsdfrom2 = "[\"LSD\",[],{\"token\":\""
Write is as a string literal with " to escape the quotes.
string mystring = #"[""LSD"",[],{""token"":""";
I believe you are looking to escape the quotation marks?
string lsdfrom2 = "[\"LSD\",[],{\"token\":\"";
See: Escape Sequences
You need to escape the quotes
String lsdfrom2 = "[\"LSD\",[],{\"token\":\"" // escaping the quotes..
This question already has answers here:
How do you remove repeated characters in a string
(7 answers)
Closed 10 years ago.
I have a string given by user. After the user entry i want the character '-' to appear only once even if appears twice or more.
DF--JKIL-L should be DF-JKIL-L
`DF-----JK-L-` should be `DF-JK-L-`
A simple regular expression should do the trick:
string originalString = "DF-----JK-L-";
string replacedString = Regex.Replace(originalString, "-+", "-");
You can use Split with option StringSplitOptions.RemoveEmptyEntries, then Join again:
var result = string.Join("-",
input.Split(new[] {'-'}, StringSplitOptions.RemoveEmptyEntries));
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
C# code to linkify urls in a string
I'm sure this is a stupid question but I can't find a decent answer anywhere. I need a good URL regular expression for C#. It needs to find all URLs in a string so that I can wrap each one in html to make it clickable.
What is the best expression to use for this?
Once I have the expression, what is the best way to replace these URLs with their properly formatted counterparts?
Thanks in advance!
I am using this right now:
text = Regex.Replace(text,
#"((http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,#?^=%&:/~\+#]*[\w\-\#?^=%&/~\+#])?)",
"<a target='_blank' href='$1'>$1</a>");
Use this code
protected string MakeLink(string txt)
{
Regex regx = new Regex("http://([\\w+?\\.\\w+])+([a-zA-Z0-9\\~\\!\\#\\#\\$\\%\\^\\&\\*\\(\\)_\\-\\=\\+\\\\\\/\\?\\.\\:\\;\\'\\,]*)?", RegexOptions.IgnoreCase);
MatchCollection mactches = regx.Matches(txt);
foreach (Match match in mactches)
{
txt = txt.Replace(match.Value, "<a href='" + match.Value + "'>" + match.Value + "</a>");
}
return txt;
}