How to use regular expressions to get data

  • Thread starter Thread starter Paula
  • Start date Start date
P

Paula

Hi !!

What regular expression should I write to get data from an html tag?

Example: <title> The data I want to retrieve </title>

I want to get "The data I want to retrieve"

Could be right this one? @<\s* (title)\s* > [(\s* \w+)]+ (</title>)+

Thanks
 
Paula said:
What regular expression should I write to get data from an html tag?

Example: <title> The data I want to retrieve </title>

I want to get "The data I want to retrieve"

Could be right this one? @<\s* (title)\s* > [(\s* \w+)]+ (</title>)+

Try this:

Regex regex = new Regex(@"((?:<\w+>\s*)(?<data>.+)(?:\s*</\w+>))",
RegexOptions.ExplicitCapture);

which will capture your data (and only your data) to a group named
"data", ignoring beginning and trailing whitespace.
 
Ken Arway wrote:

Try this:

Regex regex = new Regex(@"((?:<\w+>\s*)(?<data>.+)(?:\s*</\w+>))",
RegexOptions.ExplicitCapture);

which will capture your data (and only your data) to a group named
"data", ignoring beginning and trailing whitespace.

Ooops, should have used non-greedy:

Regex regex = new Regex(@"((?:<\w+>\s*)(?<data>.+?)(?:\s*</\w+>))",
RegexOptions.ExplicitCapture);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

C# and regex continued 1
Mp3 tagging 4
regular expression NxM 9
C# and regex issue 7
Excel Move or Copy Stopped Working? 0
Search STring in Regular Expression 4
Regular expression 5
Regular Expression Question 7

Back
Top