views:

81

answers:

2

Hello

I want to get the title of a web page using C#. So i'm requesting that page and getting the response page and using a regular expression to extract the title.

string regex = @"(?<=)([\s\S]*)(?=)/";

But i saw a site having two titles such as

<title>Page Title</title>
<link href="...."/>
<title>Page Title</title>

So when i use the above Regex i get the title as

Page Title</title><link href="...."/><title>Page Title

I know that Regular expressions are greedy. How to modify the Regex as ungreedy so that it gives me "Page Title"?

+3  A: 

To ungreed your * you can use *?

Colin Hebert
Thanks. Worked perfect. Dint know it is that simple.
NLV
+2  A: 
(?<=(<\s*title\s*>))((\s|\S)*?)(?=<\s*\/\s*title\s*>)
Ahsan Iqbal