Solved

replace <select>

Posted on 2012-03-29
4
204 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thank you very much.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

744 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now