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# ?
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.
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?
the regex is
/dogs/i
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.
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);
}
}
}
看起来您只需要使用StartsWith方法。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.