C# Regex replace is not working [duplicate] - c#

This question already has answers here:
Escape Special Character in Regex
(3 answers)
Closed 5 years ago.
I am trying to use RegEx to replace a string, but nothing is being replaced. I'm not sure what I'm doing wrong.
System.Text.RegularExpressions.Regex regEx = null;
regEx = new System.Text.RegularExpressions.Regex("DefaultStyle.css?x=839ua9");
pageContent = regEx.Replace(htmlPageContent, "DefaultStyleSnap.css?x=742zh2");
htmlPageContent is of type string, and contains the html page content of the Render method (write before we send it to the browser). The htmlPageContent string definitely contains "DefaultStyle.css?x=839ua9". I can see it when I do Quick Watch in visual studio, and when I view Page Source.
Why is RegEx not replacing the string?

You have to use \? and \. instead of ? and . in your Regex. Please check below code.
CODE:
using System;
public class Program
{
public static void Main()
{
string htmlPageContent = "Some HTML <br> DefaultStyle.css?x=839ua9";
string pageContent = "";
System.Text.RegularExpressions.Regex regEx = null;
//regEx = new System.Text.RegularExpressions.Regex("DefaultStyle.css?x=839ua9");
regEx = new System.Text.RegularExpressions.Regex(#"DefaultStyle\.css\?x=839ua9");
pageContent = regEx.Replace(htmlPageContent, "DefaultStyleSnap.css?x=742zh2");
Console.WriteLine(pageContent);
}
}
Output:
Some HTML <br> DefaultStyleSnap.css?x=742zh2
You can check the output in DotNetFiddle.

You need to escape your special characters using \.
System.Text.RegularExpressions.Regex regEx = null;
regEx = new System.Text.RegularExpressions.Regex("DefaultStyle\.css\?x=839ua9");
pageContent = regEx.Replace(htmlPageContent, "DefaultStyleSnap.css?x=742zh2");
You need to explicitly escape the ? which means 0 or more of the previous character.
As for the . if you do not escape it, you will match any characters. It would be more precise to escape it as well to make sure you don't match something like
DefaultStyle-css?x=839ua9

Related

Regular Expression for a middle string

I need to extract from the below string
2_240219_0.vnd as 240219
I have tried as follows: _[0-9]+_
This gives me _240219_
How do I remove the _ from both ends.
I would actually recommend not even using regex in this case. A simple string split on underscore should do just fine:
string input = "2_240219_0.vnd";
string middle = input.Split('_')[1];
Console.WriteLine(middle);
240219
You can try using a other regex: ([\d]{6,})
Match m = Regex.Match(2_240219_0.vnd, `([\d]{6,})`, RegexOptions.IgnoreCase);

Replace string between specific characters [duplicate]

This question already has answers here:
How to replace the text between two characters in c#
(7 answers)
Closed 5 years ago.
I was wondering how do I use regex to replace string between two characters.
var oldString = "http://localhost:61310/StringtoConvert?Id=1"
Expected result = "http://localhost:61310/ConvertedString?Id=1"
No need for regex or string operations, use UriBuilder class.
var oldString = "http://localhost:61310/StringtoConvert?Id=1";
var newuri = new UriBuilder(new Uri(oldString));
newuri.Path = "ConvertedString";
var result = newuri.ToString();
You can use Regex.Replace(string, string, string). So, if you want to replace a substring between a / and a ?, you can use
string result = Regex.Replace(oldString, "(?<=\/)[^\?]*(?=\?)", "ConvertedString");
?<= is a lookbehind, \/ escapes the slash character, [^\?]* matches any characters not a ? any number of times, ?= is a lookahead, and \? escapes the question mark character.
Instead of Regex, you can use the System.Uri class then concatenate or interpolate your new value:
private static string ReplacePath(string url, string newPath)
{
Uri uri = new Uri(url);
return $"{uri.GetLeftPart(UriPartial.Authority)}/{newPath}{uri.Query}";
}
Calling this method with your url, and the new path (ie "ConvertedString") will result in the output: "http://localhost:61310/ConvertedString?Id=1"
Fiddle here.
EDIT
#Eser's answer is way better than mine. Did not know that class existed. Mark their answer as the right one not mine.

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.

Extracting a string between two characters

How do I extract a string between two characters using a regular expression?
For example:
{sdjhjkfd 78983njdsjnc cxjkc/m/xc;dfo}/mnvckjdf{jhdfkjhxbbnkhfd}
How to get string between { and }?
Regex regexObj = new Regex(#"(?<=\{)[^{}]*(?=\})");
allMatchResults = regexObj.Matches(subjectString);
gets you all text between innermost, correctly balanced braces. No escaped braces are allowed.

Regular expression to remove HTML tags

I am using the following Regular Expresion to remove html tags from a string. It works except I leave the closing tag. If I attempt to remove: blah it leaves the <a/>.
I do not know Regular Expression syntax at all and fumbled through this. Can someone with RegEx knowledge please provide me with a pattern that will work.
Here is my code:
string sPattern = #"<\/?!?(img|a)[^>]*>";
Regex rgx = new Regex(sPattern);
Match m = rgx.Match(sSummary);
string sResult = "";
if (m.Success)
sResult = rgx.Replace(sSummary, "", 1);
I am looking to remove the first occurence of the <a> and <img> tags.
Using a regular expression to parse HTML is fraught with pitfalls. HTML is not a regular language and hence can't be 100% correctly parsed with a regex. This is just one of many problems you will run into. The best approach is to use an HTML / XML parser to do this for you.
Here is a link to a blog post I wrote awhile back which goes into more details about this problem.
http://blogs.msdn.com/b/jaredpar/archive/2008/10/15/regular-expression-limitations.aspx
That being said, here's a solution that should fix this particular problem. It in no way is a perfect solution though.
var pattern = #"<(img|a)[^>]*>(?<content>[^<]*)<";
var regex = new Regex(pattern);
var m = regex.Match(sSummary);
if ( m.Success ) {
sResult = m.Groups["content"].Value;
To turn this:
'<td>mamma</td><td><strong>papa</strong></td>'
into this:
'mamma papa'
You need to replace the tags with spaces:
.replace(/<[^>]*>/g, ' ')
and reduce any duplicate spaces into single spaces:
.replace(/\s{2,}/g, ' ')
then trim away leading and trailing spaces with:
.trim();
Meaning that your remove tag function look like this:
function removeTags(string){
return string.replace(/<[^>]*>/g, ' ')
.replace(/\s{2,}/g, ' ')
.trim();
}
In order to remove also spaces between tags, you can use the following method a combination between regex and a trim for spaces at start and end of the input html:
public static string StripHtml(string inputHTML)
{
const string HTML_MARKUP_REGEX_PATTERN = #"<[^>]+>\s+(?=<)|<[^>]+>";
inputHTML = WebUtility.HtmlDecode(inputHTML).Trim();
string noHTML = Regex.Replace(inputHTML, HTML_MARKUP_REGEX_PATTERN, string.Empty);
return noHTML;
}
So for the following input:
<p> <strong> <em><span style="text-decoration:underline;background-color:#cc6600;"></span><span style="text-decoration:underline;background-color:#cc6600;color:#663333;"><del> test text </del></span></em></strong></p><p><strong><span style="background-color:#999900;"> test 1 </span></strong></p><p><strong><em><span style="background-color:#333366;"> test 2 </span></em></strong></p><p><strong><em><span style="text-decoration:underline;background-color:#006600;"> test 3 </span></em></strong></p>
The output will be only the text without spaces between html tags or space before or after html:
"   test text   test 1  test 2  test 3 ".
Please notice that the spaces before test text are from the <del> test text </del> html and the space after test 3 is from the <em><span style="text-decoration:underline;background-color:#006600;"> test 3 </span></em></strong></p> html.
Strip off HTML Elements (with/without attributes)
/<\/?[\w\s]*>|<.+[\W]>/g
This will strip off all HTML elements and leave behind the text. This works well even for malformed HTML elements (i.e. elements that are missing closing tags)
Reference and example (Ex.10)
So the HTML parser everyone's talking about is Html Agility Pack.
If it is clean XHTML, you can also use System.Xml.Linq.XDocument or System.Xml.XmlDocument.
can use:
Regex.Replace(source, "<[^>]*>", string.Empty);
If you need to find only the opening tags you can use the following regex, which will capture the tag type as $1 (a or img) and the content (including closing tag if there is one) as $2:
(?:<(a|img)(?:\s[^>]*)?>)((?:(?!<\1)[\s\S])*)
In case you have also closing tag you should use the following regex, which will capture the tag type as $1 (a or img) and the content as $2:
(?:<(a|img)(?:\s[^>]*)?>)\s*((?:(?!<\1)[\s\S])*)\s*(?:<\/\1>)
Basically you just need to use replace function on one of above regex, and return $2 in order to get what you wanted.
Short explanation about the query:
( ) - is used for capturing whatever matches the regex inside the brackets. The order of the capturing is the order of: $1, $2 etc.
?: - is used after an opening bracket "(" for not capturing the content inside the brackets.
\1 - is copying capture number 1, which is the tag type. I had to capture the tag type so closing tag will be consistent to the opening one and not something like: <img src=""> </a>.
\s - is white space, so after opening tag <img there will be at least 1 white space in case there are attributes (so it won't match <imgs> for example).
[^>]* - is looking for anything but the chars inside, which in this case is >, and * means for unlimited times.
?! - is looking for anything but the string inside, kinda similar to [^>] just for string instead of single chars.
[\s\S] - is used almost like . but allow any whitespaces (which will also match in case there are new lines between the tags). If you are using regex "s" flag, then you can use . instead.
Example of using with closing tag:
https://regex101.com/r/MGmzrh/1
Example of using without closing tag:
https://regex101.com/r/MGmzrh/2
Regex101 also has some explanation for what i did :)
You can use already existing libraries to strip off the html tags. One good one being Chilkat C# Library.
If all you're trying to do is remove the tags (and not figure out where the closing tag is), I'm really not sure why people are so fraught about it.
This Regex seems to handle anything I can throw at it:
<([\w\-/]+)( +[\w\-]+(=(('[^']*')|("[^"]*")))?)* *>
To break it down:
<([\w\-/]+) - match the beginning of the opening or closing tag. if you want to handle invalid stuff, you can add more here
( +[\w\-]+(=(('[^']*')|("[^"]*")))?)* - this bit matches attributes [0, N] times (* at then end)
+[\w\-]+ - is space(s) followed by an attribute name
(=(('[^']*')|("[^"]*")))? - not all attributes have assignment (?)
('[^']*')|("[^"]*") - of the attributes that do have assignment, the value is a string with either single or double quotes. It's not allowed to skip over a closing quote to make things work
*> - the whole thing ends with any number of spaces, then the closing bracket
Obviously this will mess up if someone throws super invalid html at it, but it works for anything valid I've come up with yet. Test it out here:
const regex = /<([\w\-/]+)( +[\w\-]+(=(('[^']*')|("[^"]*")))?)* *>/g;
const byId = (id) => document.getElementById(id);
function replace() {
console.log(byId("In").value)
byId("Out").innerText = byId("In").value.replace(regex, "CUT");
}
Write your html here: <br>
<textarea id="In" rows="8" cols="50"></textarea><br>
<button onclick="replace()">Replace all tags with "CUT"</button><br>
<br>
Output:
<div id="Out"></div>
Remove image from the string, using a regular expression in c# (image search performed by image id)
string PRQ=<td valign=\"top\" style=\"width: 400px;\" align=\"left\"><img id=\"llgo\" src=\"http://test.Logo.png\" alt=\"logo\"></td>
var regex = new Regex("(<img(.+?)id=\"llgo\"(.+?))src=\"([^\"]+)\"");
PRQ = regex.Replace(PRQ, match => match.Groups[1].Value + "");
Why not trying reluctant quantifier?
htmlString.replaceAll("<\\S*?>", "")
(It's Java but the main thing is to show the idea)
Simple way,
String html = "<a>Rakes</a> <p>paroladasdsadsa</p> My Name Rakes";
html = html.replaceAll("(<[\\w]+>)(.+?)(</[\\w]+>)", "$2");
System.out.println(html);
Here is the extension method I've been using for quite some time.
public static class StringExtensions
{
public static string StripHTML(this string htmlString, string htmlPlaceHolder) {
const string pattern = #"<.*?>";
string sOut = Regex.Replace(htmlString, pattern, htmlPlaceHolder, RegexOptions.Singleline);
sOut = sOut.Replace(" ", String.Empty);
sOut = sOut.Replace("&", "&");
sOut = sOut.Replace(">", ">");
sOut = sOut.Replace("<", "<");
return sOut;
}
}
This piece of code could help you out easily removing any html tags:
import re
string = str(blah)
replaced_string = re.sub('<a.*href="blah">.*<\/a>','',string) // remember, sub takes 3 arguments.
Output is an empty string.
Here's an extension method I created using a simple regular expression to remove HTML tags from a string:
/// <summary>
/// Converts an Html string to plain text, and replaces all br tags with line breaks.
/// </summary>
/// <returns></returns>
/// <remarks></remarks>
[Extension()]
public string ToPlainText(string s)
{
s = s.Replace("<br>", Constants.vbCrLf);
s = s.Replace("<br />", Constants.vbCrLf);
s = s.Replace("<br/>", Constants.vbCrLf);
s = Regex.Replace(s, "<[^>]*>", string.Empty);
return s;
}
Hope that helps.
Select everything except from whats in there:
(?:<span.*?>|<\/span>|<p.*?>|<\/p>)

Categories

Resources