.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Gaurav Pal
Post New Web Links

Regex.replace [h2] to

Posted By:      Posted Date: August 25, 2010    Points: 0   Category :ASP.Net

I try to replace [h2] to <h2>
I use this coding:

string o = "<h2>test</h2>";
o = Regex.Replace(o, "[h2]", "<h2>");
o = Regex.Replace(o, "[/h2]", "</h2>");


But the result after the first regex is:


So that is not what i want.
After the first 1, he should make:


Whats wrong?

View Complete Post

More Related Resource Links

regex replace - but with a few exceptions.


I have a string containing HTML and I need to replace some words to be links (I know, working with regex against HTML is not accepted by all developers, but I cannot see an easy solution with any other technique) - I do this with the following code;

string lNewHTML = Regex.Replace(lOldHTML, "(\bword1\b|\bword2|word3\b)", "<a href=\"page.aspx#$1\">$1</a>", RegexOptions.IgnoreCase);

The code works, but I need to include some exceptions to the replace - e.g. I will not replace anything i an img-, li- and a-tag (including link-text and attributes like href and title) but still allow replacements in p-, td- and div-tags.

By using the following code I can avoid replacing in attributes;


But with the above code it still replaces words in e.g. links and headlines which is a no-go.

The last example I have seems to do what I need - but if my HTML-string contains more than a few characters my application crashes;


Can anyone figure out a solution to this?

Replace space from string - REGEX.



I have some problem with RegEx.

I have text:

"something here [tagQuote=Name of Author]bla bla bla[/tagQuote] something here"

And now I want to replace all the whitespace character in quote attribute with "%#%#%#%".

So I want to have: [tagQuote=Name%#%#%#%of%#%#%#%Author]bla bla bla[/tagQuote]

So remove all of the whitespaces between "[tagQuote=" and "]...[/tagQuote]"

Need Regex.Replace help for lowercase to uppercase



I need help with Regex replacement.  I searched the web and tried for several hours but I just can't get it working. 

In theory, what I need to do is simple: make sure all element names in HTML tags are uppercase, e.g.:




If anyone could help me out I would greatly appreciate it!  


Using Regex.Replace



I am reading in a file and performing a search & replace for specific matches. 

Is it possible to do the following with regular expressions? NOTE: the comma and slash being inserted in the output.

strLine = Regex.Replace(strLine, "<?data [REGEX HERE]?>", "<?data,\...\?>");

Input:  <?data ...?>

Output: <?data,\...\?>


RegEx.Replace - why is capture group 1 empty?


Using PowerShell to illustrate:

$t = '<img src="r1.jpg" width="330" class="image">'
$pattern = 'src[\s]*?=[\s]*?[''|"](.+?jpg)[''|"][\s]*?width=[''|"]330[''|"]'

[RegEx]::Match($t, $pattern)


Groups   : {src="r1.jpg" width="330", r1.jpg}
Success  : True
Captures : {src="r1.jpg" width="330"}
Index    : 5
Length   : 24
Value    : src="r1.jpg" width="330"

Just to confirm,


gives: r1.jpg



RegEx to get links attributes in order to replace


I have anchor tags which are unusually formatted.

I firstly I have tags which are as below <a name="1">text</a>

I need to get the value from the name attribute.

I then need to get the whole 1st part of the tag <a name="1"> so I can replace it with a properly formated tag i.e. <a href="URL" name="1">

I am a little lost in the world of Reg Ex and all the examples presume that there is a "href" followed by a formatted URL present.


As you can see I have to c# code to replace the regex output but just dont know where to start with the pattern.

public String replaceID_withLinksrcs(String Text, IRepository<Corp.Web.Model.Link> objLinks)
String parsedtext;
parsedtext = Text.Replace("shape=\"rect\"","");
String t18 = @parsedtext;
String p18 = @"/<^a name= $>";
t18 = Regex.Replace(t18, p18, new MatchEvaluator(delegate(Match match) { return MyLinkURLS(match, objLinks); }));
return t18;


Can I replace the EntityObject as base-class in Entity Designer 3.5 sp1?


I'm using EF3.5sp1 for a project, and I've been working around many of its problems... as such, my entity partial classes have a lot of logic in them for change-tracking and whatnot.  Obviously, I'd like to factor this logic out into a common base class, but of course all my objects must derive from EntityObject.  Is it possible to tell them to derive from some class of my own invention (which would actually likely derive from EntityObject themselves)?

I'd rather avoid editing the EDMX file unless the changes will be safe on entity-model-update, since I'm still making changes to the database schema in this early state of my project.


Also, is there any good workaround for the fact that the change isn't tracked when you change EntityReference.EntityKey?

Regex pattern for validating IPv6


Can anyone can make a regular expression pattern for validating IPv6 (not IPv4)?

Ipv6 has many formats and aliases. I tried making the pattern but written in a lot of codes (i'm not sure my code below will be 100% accepting valid formats and 100% declines invalid formats).


Replace text in a word document stored in a Document Library


Hi to all,

im tying to replace some text introduced in a word document stored in a document library.

Exactly i need a web part with a button that change a key in a word template <list> (for example) and replace <list> with a text in another list.


I think that it would works with the Microsoft.Office.Word.Interop; namespace but when i call the function :

wDoc = wordApp.Documents.Open(ref fileName, ref missing,
                                ref readOnly, ref missing, ref missing, ref missing,
                                ref missing, ref missing, ref missing, ref missing,
                                ref missing, ref isVisible, ref missing, ref missing,

regex: How to allow a comma?

Hi,How to allow a comma?"^ \ $? (\ D (1.3),? (\ D (3 },?)* \ d (3) (\. \ D (0.2))? | \ D (1.3) (\. \ d (0.2))? | \. \ d (1.2 }?)$"  This is works, I need character ","Helpthanks

The replace of web application stress tool

Hi i want  know in 2010 what is the replace of web application stress tool. what is the new tool for testing application , not Microsoft test manager,  with out a tema foundatin server a application like jmeter   thamks

multi-line regex in C#

Hi, I am sure this is really simple, but i can't figure it out. I have a string that looks like this: REV=A1 STATUS=Working I would like to grab 'A1' and 'Working', in a single regex, but i cannot seem to handle the linebreak. I have tried putting something together in Rad's Regular expressions designer and this pattern will match it all: REV=(?<Rev>.{2}) STATUS=(?<Status>.+) but i don't know to write a two-lined regex in C#. I have also tried to include the linebreak, with both \n and \r, but none of them worked. So, how is my pattern suppoed to look for this?Nicolai Søndergaard LM Glasfiber A/S

I need a regex for the following sentence

I have a line of text which is as follows  : This should occur before variable1 = 1 variable2 =2 variable3=6 variable4=9   I want a regex which checks if this text (This should occur before ) comes before  the string of the variables (variable1, variable2, variable3, variable4 ) and also  should pass if the placement of the variables can occur anywhere in the sentence.Also the regex should pass only if the variable4 has a value greater than 9.  

Regex for checking number greater than 9

Hi All , I need to write a regex which can check for a number greater than equal to 9 .  

Change case using Find & Replace dialog box

Hi, I want to change case of link URLs using Find & Replace dialog box . Is it possible? eg changing http://www.abc.com/S pecial-E dition.aspx, http://www.abc.com/O ther-N ews.aspx to http://www.abc.com/s pecial-e dition.aspx, http://www.abc.com/o ther-n ews.aspx

Exclude a word regex

Currently i am using one regex for accepting some records from a file.  That regex is ^[a-zA-Z0-9| ].*$. This regex accepts record starting with alphanumerics or blank space.I want to modify this regex such that, previous conditions should be there and regex should not accept record starting with word 'bbb'. Which regex should be used?

Find and replace link source

I want to replace links href in the source code using regular expression in Find/Replace dialog box from http://www.abc.com/car-news/13PX5S/News1.aspx, http://www.abc.com/car-news/27JXW5/News2.aspx to http://www.abc.com/car-news/news1/13px5s/, http://www.abc.com/car-news/news2/27jxw5/   - in the resultant URL there will be no Capital letters.    
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend