Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

replace <select>

Posted on 2012-03-29
4
Medium Priority
?
215 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

715 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