473,405 Members | 2,444 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Using Regular Expressions

Hi,
I have a text file full of html tags. I would like to extract the
text that is situated between two specific tags. The two tags are
<span class="serif"> ????? </span>.

Now I would like to get the text that is marked as ?. Its length is
not predefined.
Can someone help me out.

Thanks in Advance
Nov 16 '05 #1
6 2187
be*********@yahoo.com (Xarky) wrote in
news:bc*************************@posting.google.co m:
Hi,
I have a text file full of html tags. I would like to extract
the
text that is situated between two specific tags. The two tags
are
<span class="serif"> ????? </span>.

Now I would like to get the text that is marked as ?. Its
length is
not predefined.


Xarky,
using System.Text.RegularExpressions;

....

Match m = Regex.Match(htmlText,
@"<\s*?span.*?class\s*?=\s*?""serif"".*?[^>]*?>(?<contents>.*?)</\s*?span\s*?>",
RegexOptions.Singleline |
RegexOptions.IgnoreCase);

return m.Groups["contents"].ToString();
--
Hope this helps.

Chris.
-------------
C.R. Timmons Consulting, Inc.
http://www.crtimmonsinc.com/
Nov 16 '05 #2
"Chris R. Timmons" <crtimmons@X_NOSPAM_Xcrtimmonsinc.com> wrote in
message news:Xn**********************************@207.46.2 48.16...
Match m = Regex.Match(htmlText,

@"<\s*?span.*?class\s*?=\s*?""serif"".*?[^>]*?>(?<contents>.*?)</\s*?span\s*?>",
RegexOptions.Singleline |
RegexOptions.IgnoreCase);

return m.Groups["contents"].ToString();


Hi Chris,

I understand the string excepting for the "*?"s What is the impact of the
"?" ?
How does the behaviour differ from
@"<\s*span.*class\s*=\s*""serif"".*[^>]*>(?<contents>.*)</\s*span\s*>",

thanks
Barry Mossman
Nov 16 '05 #3
"Barry Mossman" <BM************@gmail.com> wrote in
news:ew**************@TK2MSFTNGP11.phx.gbl:
"Chris R. Timmons" <crtimmons@X_NOSPAM_Xcrtimmonsinc.com> wrote
in message
news:Xn**********************************@207.46.2 48.16...
Match m = Regex.Match(htmlText,

@"<\s*?span.*?class\s*?=\s*?""serif"".*?[^>]*?>(?<contents>.*?)<
/\s*?span\s*?>",
RegexOptions.Singleline |
RegexOptions.IgnoreCase);

return m.Groups["contents"].ToString();


Hi Chris,

I understand the string excepting for the "*?"s What is the
impact of the "?" ?
How does the behaviour differ from
@"<\s*span.*class\s*=\s*""serif"".*[^>]*>(?<contents>.*)</\s*span
\s*>",


Barry,

Normally, quantifiers like * and + are "greedy", which means the
regex will match as many characters as possible. The ? makes the
regex "non-greedy", so the expression will only match the minimum
amount of characters necessary. (Note that in the regex I posted,
the \s*? is equivalent to \s*. I kind of went overboard with the ?s
:-) ).

For example, assume the following input:

<text>first</text><text>second</text>

Let's say I want to extract the text between the first set of <text>
tags. Using this greedy expression:

<text>(.*)</text>

would return this text:

first</text><text>second

See how the .* matched as much as possible?

If the regex is changed to:

<text>(.*?)</text>

then only the minimum amount of text is matched:

first

--
Hope this helps.

Chris.
-------------
C.R. Timmons Consulting, Inc.
http://www.crtimmonsinc.com/
Nov 16 '05 #4
"Chris R. Timmons" <crtimmons@X_NOSPAM_Xcrtimmonsinc.com> wrote in
message news:Xn**********************************@207.46.2 48.16...
Hope this helps.


Yes, lots. Thanks

Barry Mossman
Nov 16 '05 #5

Thanks that helped alot.

Now suppose I have

<text><I>......<I></text>
and I would like to extract the text between the <text> tags, but remove
any possible tags.

I am doing it as follows -

First extracting the text and then applying this
string newText = Regex.Replace(text,"<[^>]*>","");

Is this the correct way to do it?
Thanks

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #6
xarky <nospam@in_the_sea.kills.xarky> wrote in
news:uQ**************@TK2MSFTNGP10.phx.gbl:

Thanks that helped alot.

Now suppose I have

<text><I>......<I></text>
and I would like to extract the text between the <text> tags,
but remove any possible tags.

I am doing it as follows -

First extracting the text and then applying this
string newText = Regex.Replace(text,"<[^>]*>","");

Is this the correct way to do it?


xarky,

Yes.
Chris.
-------------
C.R. Timmons Consulting, Inc.
http://www.crtimmonsinc.com/
Nov 16 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Ron Brennan | last post by:
Good morning. I would like to use one or more RegExp to validate country names as having the first and last words beginning with an uppercase letter, intermediate words beginning with either...
8
by: Michael McGarry | last post by:
Hi, I am horrible with Regular Expressions, can anyone recommend a book on it? Also I am trying to parse the following string to extract the number after load average. ".... load average:...
2
by: Bob | last post by:
Let me state up front that I know very little about XML. My experience is pretty much limited using the XML Serializer to serialize a user preferences class to a file and back again. I'm writing...
1
by: Daniel Walzenbach | last post by:
Hi, does anybody know I can extract a substring of a text with regular expressions. Let’s consider the following text: “Regular expressions are often used to make sure that a string matches a...
4
by: Együd Csaba | last post by:
Hi All, I'd like to "compress" the following two filter expressions into one - assuming that it makes sense regarding query execution performance. .... where (adate LIKE "2004.01.10 __:30" or...
20
by: Asper Faner | last post by:
I seem to always have hard time understaing how this regular expression works, especially how on earth do people bring it up as part of computer programming language. Natural language processing...
0
by: peridian | last post by:
Hi, I wanted a web page where I could post code to, and have it appear in coloured formatting based on the context of the code. Most of the techniques I have seen for this involve complex use...
1
by: Allan Ebdrup | last post by:
I have a dynamic list of regular expressions, the expressions don't change very often but they can change. And I have a single string that I want to match the regular expressions against and find...
12
by: FAQEditor | last post by:
Anybody have any URL's to tutorials and/or references for Regular Expressions? The four I have so far are: http://docs.sun.com/source/816-6408-10/regexp.htm...
9
by: Rene | last post by:
I'm trying to basically remove chunks of html from a page but I must not be doing my regular expression correctly. What i'm trying with no avail. $site = preg_replace("/<!DOCTYPE(.|\s)*<div...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.