Regex to replace a string between special characters in c# - c#

I have a situation where I have a string in which I have to replace a part that lies between special characters.
I can do the same using substrings and length,but that is the dirty way.
Is there any better way of doing this using regex?
e.g. of the string is
string str1 = "This is the <![CDATA[ SampleDataThatNeedsToBeReplaced ]]";
string repl = "Replacement Text";
I need a regex to get the output as
This is the Replacement Text
I did try a few regex like the following
result = Regex.Replace(str1, #"(?<=CDATA\[)(\w+?)(?=\]\])", repl);
I also tried
Regex x = new Regex("(\\[CDATA\\])(.*?)(\\[\\]\\]\\])");
string Result = str1.Replace(text, "$1" + repl + "$3");
did not get any results.
Any help is appreciated.

Regex.Replace (
"This is the <![CDATA[ SampleDataThatNeedsToBeReplaced ]]",
#"<!\[CDATA\[(.+)]]",
"Replacement Text");
Note that in case you need it ; the old text (between the inner brackets) is available as group1 (and so can be referenced via $1)

Related

grab text between two patern

Check the code bellow. I want to grab everything between this id="a-popover-sp-info-popover- until ". I already tried to use following Regex.Match formula but there is syntax error. Its not valid in c#. How can i do this in proper way. My goal is to grab ABC123 text.
string foo = #id="a-popover-sp-info-popover-ABC123";
string output = Regex.Match(foo, #"id="a-popover-sp-info-popover-(.*)"").Groups[1].Value;
i need to grab only text: ABC123
since your pattern is so rigid, actually the string.Split method could also do the trick:
string output1 = foo.Split(new string[] {"info-popover-"}, StringSplitOptions.RemoveEmptyEntries)
.Last()
.TrimEnd('"');
Console.WriteLine(output1);
Output:
ABC123
You have to make sure to surround your strings with quotation marks ".
If you want to have quotation marks inside of your string you have to escape them with a backslash:
string foo = "id=\"a-popover-sp-info-popover-ABC123\"";
string output = Regex.Match(foo, "id=\"a-popover-sp-info-popover-(.*)\"").Groups[1].Value;
string pattern = "id=\"a-popover-sp-info-popover-[A-Z]{3}[1-9]{3}\"";
string input = "id=\"a-popover-sp-info-popover-ABC123\"";
Match m = Regex.Match(input, pattern);
if (m.Success) Console.WriteLine("Found '{0}'", m.Value);

How to remove multiple first characters using regex?

I have string string A = "... :-ggw..-:p";
using regex: string B = Regex.Replace(A, #"^\.+|:|-|", "").Trim();
My Output isggw..p.
What I want is ggw..-:p.
Thanks
You may use a character class with your symbols and whitespace shorthand character class:
string B = Regex.Replace(A, #"^[.:\s-]+", "");
See the regex demo
Details
^ - start of string
[.:\s-]+ - one or more characters defined in the character class.
Note that there is no need escaping . inside [...]. The - does not have to be escaped since it is at the end of the character class.
A regex isn't necessary if you only want to trim specific characters from the start of a string. System.String.TrimStart() will do the job:
var source = "... :-ggw..-:p";
var charsToTrim = " .:-".ToCharArray();
var result = source.TrimStart(charsToTrim);
Console.WriteLine(result);
// Result is 'ggw..-:p'

.Net regex for string between \" "\

I have been trying to get the id from the following text
<body id=\"body\" runat=\"server\">
In C# using substring or even Regex, but nothing seems to be working. No matter what regex i use, i always get the whole line back. I have been trying to use ^id, ^id.*, ^id=\\\\\\\\.* and id=.* but they don't either work or give me the desired output. Is there any way i can get the id portion from this text which is enclosed between the characters \" "\?
Try this:
string htmlString = "<body id=\"body\" runat=\"server\">";
Regex regex = new Regex("id=\"(.*?)\"");
Match m = regex.Match(htmlString);
Group g = m.Groups[1];
string id = g.ToString();
Console.WriteLine(id); //body
Test here:
http://rextester.com/BQSF93427

Regex expression to replace text between start and end characters, but keep characters too

How can I replace text using c# regex that starts with "<" and ends with ">", but keep start and end characters and suround found match with {} brackets?
All occurrences in text should be replaced.
For example:
This is <my> long <text> should become
This is {<my>} long {<text>}.
Thomas is correct -- in this case, you do not need a regular expression. However, if you insist on using one (or want to expand this logic in the future to handle a range of characters), here it is:
var inputString = "This is <my> long <text>";
var newInputString = Regex.Replace(inputString, "(<[^>]+>)", "{$1}");
This regex assumes you are capturing at least one character between the angled brackets.
Why don't you use just replace;
string text = "This is <my> long <text>";
var replacedText = text.Replace("<", "{<").Replace(">", ">}");
If you have encoded text, you can decode it first;
string text = "This is <my> long <text&gt";
var replacedText = WebUtility.HtmlDecode(text).Replace("<", "{<").Replace(">", ">}");

Trim string by strings

How can I trim a string by a whole string instead of a list of single characters?
I want to remove all and whitespaces at beginning and end of an HTML string. But method String.Trim() does only have overloads for set of characters and not for set of strings.
You could use HttpUtility.HtmlDecode(String) and use the resultant as an input for String.Trim()
HttpUtility.HtmlDecode on MSDN
HttpServerUtility.HtmlDecode on MSDN (a wrapper you can access through the Page.Server property)
string stringWithNonBreakingSpaces;
string trimmedString = String.Trim(HttpUtility.HtmlDecode(stringWithNonBreakingSpaces));
Note: This solution would decode all the HTML strings in the input.
The Trim method removes from the current string all leading and trailing white-space characters by default.
Edit: Solution for your problem AFTER your edit:
string input = #" <a href='#'>link</a> ";
Regex regex = new Regex(#"^( |\s)*|( |\s)*$");
string result = regex.Replace(input, String.Empty);
This will remove all trailing and leading spaces and . You can add any string or character group to the expression. If you were to trim all tabs too the regex would simply become:
Regex regex = new Regex(#"^( |\s|\t)*|( |\s|\t)*$");
Not sure if this is what you're looking for?
string str = "hello ";
str.Replace(" ", "");
str.Trim();
Use RegEx, as David Heffernan said. It is rather easy to select all spaces at the start of string: ^(\ | )*

Categories