tags:

views:

538

answers:

6

ok i have a list of domains

for example

dogstoday . com
catstoday . com
petstoday . com
dogsnow . org
dogsabc . net
catlitter . info

i want a regex that will give me all domains that has the word i specify, for example dogs or cats

if i give dogs it should return

dogstoday.com
dogsnow.org
dogsabc.net

can any one tell me how to do this in c# ?

+2  A: 

Does this need to be done with a regex? Why not just loop over all the domains and check if they contain the word you're looking for?

String.Contains()

Chad Birch
A: 
string line;
System.Text.RegularExpressions.Regex r = new System.Text.RegularExpressions.Regex("dogs", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
using (System.IO.StreamReader reader = System.IO.File.OpenText("domains.txt"))
{
    while ((line = reader.ReadLine()) != null)
    {
        if (r.IsMatch(line))
        {
            Console.WriteLine(domain);
        }
    }
}
John JJ Curtis
the domain is a huge text file with 70,000 lines of domains
This new code sample will handle any size file just fine
John JJ Curtis
+2  A: 

the regex is

/dogs/i

Tony
A: 

looks like you just need to use the StartsWith method.

RandomNoob
the words can be in between also
+4  A: 

If the domains always start with the word you provide as in your example, you can just use StartsWith otherwise you can simply use Contains. For something simple as this you don't need regular expressions.

Brian Rasmussen
I never understood what jwz had against regular expressions, but now I think I can see where his famous quote would apply.
kch
I love regular expressions, I just don't believe they are needed for each and every task.
Brian Rasmussen
there are like 70,000 domains in a text fileand i have to remove only domains that match the word i specify.
I'm not sure I get your point here, please elaborate.
Brian Rasmussen
A regex will have to search all 70,000 domains as well if that is your concern.
Brian Rasmussen
Don't you know? Regexes are complicated, so they're better!
Chad Birch
@Chad- that's probably it :)
Brian Rasmussen
+1  A: 

Like others, I think grep would be better, but something like ...

Regex.Match( yourBigString, @".*dogs*.*[.com|.net|.org]" );

You should be careful of the domain as you might get some site like .au or .jp or whatever, but this will get anything with dogs in it followed by .com or .net or .org. You can replace "dogs" with anything you're looking for.

JP Alioto