Solved

replace <select>

Posted on 2012-03-29
4
206 Views
Last Modified: 2012-03-31
I have a string with <select></select> tag. I would like to replace all select tags (with everything in it) with  '______'.

For example "One and  <select><option value="Some text"</option></select> two and
<select><option value="Another text"</option></select>.

This should be come "One and _____ two and _____.
How can I do that?
0
Comment
Question by:johnkainn
  • 2
  • 2
4 Comments
 
LVL 23

Expert Comment

by:wdosanjos
ID: 37781805
Here is an option using Regular Expressions:
var rx = new Regex(@"<select>.*?(</select>)", RegexOptions.IgnoreCase | RegexOptions.Singleline);
var s = "One and  <select><option value=\"Some text\"</option></select> two and <select><option value=\"Another text\"</option></select>.";

var replaced = rx.Replace(s, "______");

Console.WriteLine(replaced);

Open in new window


Output:
One and  ______ two and ______.

Open in new window


I hope this helps.
0
 

Author Comment

by:johnkainn
ID: 37783089
Thank you very much. I used line to simplify.
Actually I would like to replace with different word for each select tag.
I keep the words in an array and there are the same number of items in the array as there are select tags in the string. Do you know how to do this?
0
 
LVL 23

Accepted Solution

by:
wdosanjos earned 500 total points
ID: 37783275
Please try the following:
var rx = new Regex(@"<select>.*?(</select>)", RegexOptions.IgnoreCase | RegexOptions.Singleline);
var s = "One and <select><option value=\"Some text\"</option></select> two and <select><option value=\"Another text\"</option></select>.";
var r = new String[]{"*First*", "*Second*"};

var replaced = rx.Replace(s, new MatchReplacements(r).MatchEvaluator);

Console.WriteLine(replaced);

Open in new window

MatchReplacements class:
public class MatchReplacements
{
	private String[] _replacements;
	private int _next;
	
	public MatchEvaluator MatchEvaluator { get; private set; }
	
	public MatchReplacements(String[] replacements)
	{
		_replacements = replacements;
		this.MatchEvaluator = new MatchEvaluator(ReplaceCC);
	}
		
	public string ReplaceCC(Match m)
	{
		return _replacements[_next++];
	}
}

Open in new window

Output:
One and *First* two and *Second*.

Open in new window

0
 

Author Closing Comment

by:johnkainn
ID: 37791946
Thank you very much.
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question