Solved

replace <select>

Posted on 2012-03-29
4
210 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 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …

728 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