Click here to Skip to main content
15,895,799 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm pretty new to regular expressions (oh no, a noob!)
Before you all run in the other direction :) may I please ask:

I have a list of URL's:

taxidrivermovie.com<br />
federalhousingtaxcredit.com<br />
nystax.gov<br />
tax-spot.com<br />
turbotax.com<br />
taxexemptworld.com<br />
taxidermy.net<br />
libertytax.com<br />
taxsalelists.com<br />
acttax.com


and wish to remove (replace with null) those containing the string 'taxi' to give:

federalhousingtaxcredit.com<br />
nystax.gov<br />
tax-spot.com<br />
turbotax.com<br />
taxexemptworld.com<br />
libertytax.com<br />
taxsalelists.com<br />
acttax.com


how can I achieve this using regex please?
Posted
Comments
Andreas Gieriet 20-Dec-10 17:55pm    
Graham,
Please tell what you did try out so far. Where do you want to execute the Regex? In C#, Perl, ...? Do you have a sample program to show where you get stuck?
Cheers
Andi
Dr.Walt Fair, PE 20-Dec-10 18:26pm    
If you are using .NET and by 'list' you mean a List<> then you can probably iterate through the list and delete the one where Contains("taxi") is true. Without knowing more, it's pretty hard to give a better answer.

Here is some code in C# that does not use regular expressions, but solves the problem:
C#
List<string> lstTaxi = new List<string>();
lstTaxi.Add("taxidrivermovie.com");
lstTaxi.Add("federalhousingtaxcredit.com");
lstTaxi.Add("nystax.com");
lstTaxi.Add("tax-spot.com");
lstTaxi.Add("turbotax.com");
lstTaxi.Add("taxexemptworld.com");
lstTaxi.Add("taxidermy.com");
lstTaxi.Add("libertytax.com");
lstTaxi.Add("taxsalelists.com");
lstTaxi.Add("acttax.com");

List<string> lstWithoutTaxi = new List<string>();
foreach (string str in lstTaxi)
{
    if (!str.ToUpper().Contains("TAXI"))
        lstWithoutTaxi.Add(str);
}


I hope it helps.
 
Share this answer
 
Use this regular expression (not tested, so not sure if it works):
((?!\r|\n).)*taxi((?!\r|\n).)*(\r\n|$)

And replace all found occurrances with an empty string. You could also makes some tweaks if they are important to you (e.g., handle newlines better, and make sure there are no trailing newlines).
 
Share this answer
 
I have it working now.

MANY thanks to everyone who replied - much appreciated guys.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900