Remove a substring after a substring - c#

I have a string, for example
"blabla{code}<br />blabla{code}<br />bla{code}<br />"
How to remove all entries of <br /> after {code}?
I've tried this:
public string removeBR(string comment)
{
Regex codeRegex = new Regex("{code}<br />", RegexOptions.Singleline);
return codeRegex.Replace(comment, new MatchEvaluator(m =>
{
string value = m.Groups[0].Value;
return value.Remove(value.Length - 6);
}));
}
It works but is there any easier way?

Hope this helps you
string input = "blabla{code}<br />blabla{code}<br />bla{code}<br />";
string output = input.Replace("{code}<br />", "{code}");
Console.WriteLine(output);
as String.Replace returns a new string in which all occurrences of a specified string in the current instance are replaced with another specified string.

A simple string.Replace should solve this.
string input = #"blabla{code}<br />blabla{code}<br />bla{code}<br />";
string result = input.Replace("{code}<br />", "{code}");

Related

Find substring between slashes and replace that part in ASP.NET C#

Using ASP.NET C#, I need to find and replace the word string1 between the last two slashes and replace with string2
Example:
string fullStr = "/this/is/string1/part";
string subStr = "function";
string finalStr = "/this/is/" + subStr + "/part";
And a regex solution:
string fullStr = "this/is/string1/part";
string subStr = "function";
var newstr = Regex.Replace(fullStr, #"/[^/]+/(?=[^/]+$)", m => "/" + subStr + "/");
I don't feel a need of regex here.
string fullStr = "/this/is/string1/part";
string subStr = "function";
string[] fullStrParts = fullStr.Split('/');
fullStrParts[fullStrParts.Length - 2] = subStr;
string finalStr = string.Join("/", fullStrParts);

C# Beginner: Delete ALL between two characters in a string (Regex?)

i have a string with an html code. i want to remove all html tags. so all characters between < and >.
This is my code snipped:
WebClient wClient = new WebClient();
SourceCode = wClient.DownloadString( txtSourceURL.Text );
txtSourceCode.Text = SourceCode;
//remove here all between "<" and ">"
txtSourceCodeFormatted.Text = SourceCode;
hope somebody can help me
Try this:
txtSourceCodeFormatted.Text = Regex.Replace(SourceCode, "<.*?>", string.Empty);
But, as others have mentioned, handle with care.
According to Ravi's answer, you can use
string noHTML = Regex.Replace(inputHTML, #"<[^>]+>| ", "").Trim();
or
string noHTMLNormalised = Regex.Replace(noHTML, #"\s{2,}", " ");

Replace with pattern matching

How can I use the String.Replace function to use Patterns?
What I would like to do:
newTextBox = newTextBox.Replace("<Value> #'a string of any number of chars#' </Value>",
"<Value>" + textBoxName + "</Value>");
#'a string of any number of chars#' can be any string.
Use a regular expression:
newTextBox.Text =
Regex.Replace(
newTextBox.Text,
#"<Value>[^\<]+</Value>",
"<Value>" + textBoxName.Text + "</Value>");
Could also do it like this?:
const string textBoxName = "textBoxName";
var newTextBox = "<Value>{0}</Value>".Replace("{0}", textBoxName);
You should be using Regex that's why it exists. Regex
With Regex reference:
using System.Text.RegularExpressions;
And the characters you want to replace: [0-9a-zA-Z_#' ]
newTextBox.Text = Regex.Replace(
"<Value> #'a string of any number of chars#' </Value>",
#"<Value>[0-9a-zA-Z_#' ]*</Value>",
"<Value>" + textBoxName.Text + "</Value>",
RegexOptions.IgnoreCase);

C# Regexp change link format

On my forum I have a lot of redundant link data like:
[url:30l7ypk7]http://www.box.net/shared/0p28sf6hib[/url:30l7ypk7]
In regexp how can I change these to the format:
http://www.box.net/shared/0p28sf6hib
string orig = "[url:30l7ypk7]http://www.box.net/shared/0p28sf6hib[/url:30l7ypk7]";
string replace = "$1";
string regex = #"\[url:.*?](.*?)\[/url:.*?]";
string fixedLink = Regex.Replace(orig, regex, replace);
This isn't doing it totally in Regex but will still work...
string oldUrl = "[url:30l7ypk7]http://www.box.net/shared/0p28sf6hib[/url:30l7ypk7]";
Regex regExp = new Regex(#"http://[^\[]*");
var match = regExp.Match(oldUrl);
string newUrl = string.Format("<a href='{0}' rel='nofollow'>{0}</a>", match.Value);
This should capture the string \[([^\]]+)\]([^[]+)\[/\1\] and group it so you can pull out the URL like this:
Regex re = new Regex(#"\[([^\]]+)\]([^[]+)\[/\1\]");
var s = #"[url:30l7ypk7]http://www.box.net/shared/0p28sf6hib[/url:30l7ypk7]";
var replaced = s.Replace(s, string.Format("{0}", re.Match(s).Groups[1].Value));
Console.WriteLine(replaced)
This is just from memory but I will try to check it over when I have more time. Should help get you started.
string matchPattern = #"\[(url\:\w)\](.+?)\[/\1\]";
String replacePattern = #"<a href='$2' rel='nofollow'>$2</a>";
String blogText = ...;
blogText = Regex.Replace(matchPattern, blogText, replacePattern);

Saving an XML that has invalid characters

there are code snippets that strip the invalid characters inside a string before we save it as an XML ... but I have one more problem: Let's say my user wants to have a column name like "[MyColumnOne] ...so now I do not want to strip these "[","] well because these are the ones that user has defined and wants to see them so if I use some codes that are stripping the invalid characters they are also removing "[" and "[" but in this case I still need them to be saved... what can I do?
Never mind, I changed my RegEx format to use XML 1.1 instead of XML 1.0 and now it is working good :
string pattern = String.Empty;
//pattern = #"#x((10?|[2-F])FFF[EF]|FDD[0-9A-F]|7F|8[0-46-9A-F]9[0-9A-F])"; //XML 1.0
pattern = #"#x((10?|[2-F])FFF[EF]|FDD[0-9A-F]|[19][0-9A-F]|7F|8[0-46-9A-F]|0?[1-8BCEF])"; // XML 1.1
Regex regex = new Regex(pattern, RegexOptions.IgnoreCase);
if (regex.IsMatch(sString))
{
sString = regex.Replace(sString, String.Empty);
File.WriteAllText(sString, sString, Encoding.UTF8);
}
return sString;
This worked for me, and it was fast.
private object NormalizeString(object p) {
object result = p;
if (p is string || p is long) {
string s = string.Format("{0}", p);
string resultString = s.Trim();
if (string.IsNullOrWhiteSpace(resultString)) return "";
Regex rxInvalidChars = new Regex("[\r\n\t]+", RegexOptions.IgnoreCase);
if (rxInvalidChars.IsMatch(resultString)) {
resultString = rxInvalidChars.Replace(resultString, " ");
}
//string pattern = String.Empty;
//pattern = #"";
////pattern = #"#x((10?|[2-F])FFF[EF]|FDD[0-9A-F]|7F|8[0-46-9A-F]9[0-9A-F])"; //XML 1.0
////pattern = #"#x((10?|[2-F])FFF[EF]|FDD[0-9A-F]|[19][0-9A-F]|7F|8[0-46-9A-F]|0?[1-8BCEF])"; // XML 1.1
//Regex rxInvalidXMLChars = new Regex(pattern, RegexOptions.IgnoreCase);
//if (rxInvalidXMLChars.IsMatch(resultString)) {
// resultString = rxInvalidXMLChars.Replace(resultString, "");
//}
result = string.Join("", resultString.Where(c => c >= ' '));
}
return result;
}

Categories