473,405 Members | 2,445 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.

regex pattern - ignore whitespace (CRLF and spaces)?

I have a HTML fragment that looks like this:

<tr>
<td valign="top" nowrap><span class="textBold">Property
ID: </span></td>
<td valign="top" nowrap colspan="4"
bgcolor="#F0F0F0"><b>&nbsp;01-068-24-64-1024</b></td>
</tr>

I am trying to extract the '' part of it.

This pattern works:

Property \s\ *ID: </span></td>\s\ *<td .*><b>&nbsp;(.*)</b>

I would like to simplify the pattern so that it will ignore new line
characters, &nbsp; and >1 contiguous space. Ideally, the HTML would look
like:

<tr><td valign="top" nowrap><span class="textBold">Property
ID:</span></td><td valign="top" nowrap colspan="4"
bgcolor="#F0F0F0"><b>01-068-24-64-1024</b></td></tr>

Does anyone have a suggestion on this?

Thanks a lot,

Craig Buchanan
Mar 27 '06 #1
2 6044

Craig Buchanan wrote:
I have a HTML fragment that looks like this: [snip] Does anyone have a suggestion on this?


My suggestion is to use HTMLAgilityPack, not Regex, for parsing HTML.

--
Larry Lard
Replies to group please

Mar 27 '06 #2
Hi Craig,
I am trying to extract the '' part of it.

Part of your question remains unclear to me, so I can only work with
assumptions :

1. Which HTML sample do you want to match ? The first or the second or
both ? I will assume both.
2. What part do you want to extract ? I think you missed out that part.From your Regex, you apparently want to match the part within the

<b>...</b> tags. That is what I will assume.

Here is a Regex to match : (Turn on "Dot matches Newline mode" for it
to work)

Property\s+ID:\s*</span></td>\s*<td.*?><b>(?:[&nbsp;]*)(.*)</b></td>

Points to note :
----------------------
1. Instead of \s\ * , I have used : \s+, in cases where there will be
atleast one space, and \s* where there might be zero or more spaces.
This can be changed to \s* in all cases. This matches all spaces, tabs
and line breaks.
2. If your lines break in an unanticipated position, the Regex will not
match.
3. In order to match zero or more &nbsp; special entities, I have used
(?:[&nbsp;]*). This will not store the entity in a backreference.
4. If you're using .NET, you can turn on "Dot matches newline" mode
using the RegexOptions.SingleLine option.
5. Regexes can only match very specific strings. Usually you can relax
it a bit for spaces and line breaks, but not for other characters. For
instance, if an &nbsp; is inserted anywhere else in the string, except
for within the <b>...</b> tags, the Regex will not match. So, if you're
expecting very diverse HTML fragments, you would be better off with
Larry's suggestion of using HTMLAgilityPack. It can be downloaded from
:

http://www.codefluent.com/smourier/d...gilitypack.zip

HTH,

Regards,

Cerebrus.

Mar 27 '06 #3

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

Similar topics

2
by: Robert Oschler | last post by:
One of the things that has always thrown me about Javascript regular expression patterns, is that you don't include quotes around them. So how can I modify a regex pattern at runtime. For...
1
by: Hardy Wang | last post by:
Hi all: I have a big string, format like below: A 8972057019577575 37893 7937 92 702 059032868 -86-248 6408 6048-8608175398 295830 8308 02 B A 3098453-8 3-86-386-860458602862086029...
1
by: Bill | last post by:
I have strings returned from a config file that contain zero or more name/value pairs using a pattern like "name = value;" I want to extract the value of a given name (passwords, etc.). I want...
4
by: Jéjé | last post by:
Hi, I have a file which contain 1 pair of values by line like: Name1=Value1 = I nned to store these pair of values in a sortedlist. So the result expected for the 2 samples lines is: Key ...
4
by: shonend | last post by:
I am trying to extract the pattern like this : "SUB: some text LOT: one-word" Described, "SUB" and "LOT" are key words; I want those words, everything in between and one word following the...
0
by: hardieca | last post by:
Hello, I'm creating a regex pattern that will pull out the attribute and value pairs from an HTML tag. What I have so far is: @"\s*(?<attribute>*)\s*=\s*?(?<value>*)" This is all well and...
4
by: sherifffruitfly | last post by:
Hi all, I can't see what's wrong with this regex pattern: private int ParsePageViews(string str) { int ret = 0; string pattern = @"Visits.*\n\s*Total\s\.*\s(? <visits>(\d{3})|(\d,\d{3}))";
2
by: =?Utf-8?B?QWFyb24=?= | last post by:
Hi, I'm having a tricky problem where I want to accept a regular expression pattern from user input but can't get teh escape characters to be prcoessed correctly. If I take the same pattern and...
3
by: eBob.com | last post by:
Is there a regex pattern which will match a VB.Net string? I.E. a regex which matches ... "this is a ""vb.net"" string" (I don't want three matches in this case, I want one.) I've come up...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
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.