Solved

replace <select>

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

696 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