Home » Microsoft » 70-483 » Which code should you insert at line 07?
You write the following method (line numbers are included for reference only):
You need to ensure that the method extracts a list of URLs that match the following pattern:
@http://(www.)?([^.]+).com;
Which code should you insert at line 07?
A. Option A
B. Option B
C. Option C
D. Option D
Correct Answer: A
Explanation/Reference:
The MatchCollection.GetEnumerator method returns an enumerator that iterates through a collection.
Note:
The MatchCollection Class represents the set of successful matches found by iteratively applying a regular expression pattern to the input string.
Incorrect:
Not B: The ICollection.SyncRoot property gets an object that can be used to synchronize access to the ICollection.
Reference: MatchCollection.GetEnumerator Method
https://msdn.microsoft.com/en-us/library/system.text.regularexpressions.matchcollection.getenumerator(v=vs.110).aspx
C is the correct answer
https://msdn.microsoft.com/en-us/library/b9712a7w(v=vs.110).aspx
A gives groups array. Hence Tostring(0 method mentioned above won’t give desird result
https://msdn.microsoft.com/en-us/library/system.text.regularexpressions.matchcollection(v=vs.110).aspx
static void Main(string[] args)
{
TestIfWebSite(“http://vceguide.com/”);
Console.ReadLine();
}
public static List TestIfWebSite(string url) {
const string pattern = @”http://(www\.)?([^\.]+)\.com”;
List matchResults = new List();
MatchCollection matchCollection = Regex.Matches(url, pattern);
matchResults = (from System.Text.RegularExpressions.Match m in matchCollection select m.Value).ToList();
foreach (string m in matchResults)
{
Console.WriteLine(m);
}
return matchResults;
}
Remarks
A is a stupid answer! Read microsoft:
“Instead of calling the GetEnumerator method to retrieve an enumerator that lets you iterate through the Match objects in the collection, you should use the group iteration construct …”
C
C
result = (from System.Text.RegularExpressions.Match m
in mc
select m.Value).ToList();
C
C