473,761 Members | 5,163 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

A Question About Regular Expressions and Capture

I am using regular expressions and a particular feature called "capture" (I
think) to suck some information out of some html. I could have never come
up with this myself but Balena has an example which is very similar to this.
The guts of the program is ...

Dim i As Integer
Dim rgx As Regex

Dim Pattern As String = "<td class=td1
width=""35%"">( <b>){0,1}(?<var iable>(\w| )+)</td>" + _

"\s*.*\s*<t d class=td2 width=""65%"">( <b>){0,1}(?<val ue>.+)(</b>){0,1}</td>"

Dim Pattern2 As String = "<td class=td1
width=""35%"">( <b>){0,1}(?<var iable>(\w| )+)</td>" + _

"\s*.*\s*<t d class=td2
width=""65%"">( <b>){0,1}((?<va lue>.+))(</b>){0,1}</td>" ' extra parenthesis
don't help

rgx = New Regex(Pattern)

tbxPattern.Text = Pattern

Dim m As Match, g As Group

For Each m In rgx.Matches(tbx Input.Text)

g = m.Groups("varia ble")

lstbxKeys.Items .Add(g.Value)

g = m.Groups("value ")

lstbxValues.Ite ms.Add(g.Value)

Next

The data looks like this (below). It works fine for all cases except the
first (the "Celular" data) where the value is picked up as
"123-abc-5678</b>". I want, and I think it should be, "123-abc-5678". I
can't understand why the "</b>" is included in the value. Doesn't my
pattern clearly show that the value is a string of one or more characters,
terminated by, optionally, "</b>" followed by "</td>". Is there a
straightforward way to tell it to not include the "</b>" in the value? Note
that the "</b>" is not always present so the pattern has to say that it is
optional.

Thank, Bob
<tr height=24>
<td class=td1 width="35%"><b> Celular</td>
<td width=1><img src="../img/p.gif" width=1 height=1></td>
<td class=td2 width="65%"><b> 123-abc-5678</b></td>
</tr>

<tr height=24>
<td class=td1 width="35%">Eda d</td>
<td width=1><img src="../img/p.gif" width=1 height=1></td>
<td class=td2 width="65%">24 Años</td>
</tr>

<tr height=24>
<td class=td1 width="35%">Alt ura</td>
<td width=1><img src="../img/p.gif" width=1 height=1></td>
<td class=td2 width="65%">1.7 0 mts.</td>
Jun 13 '06 #1
2 1384

eBob.com wrote:
I am using regular expressions and a particular feature called "capture" (I
think) to suck some information out of some html. I could have never come
up with this myself but Balena has an example which is very similar to this.
The guts of the program is ...

Dim i As Integer
Dim rgx As Regex

Dim Pattern As String = "<td class=td1
width=""35%"">( <b>){0,1}(?<var iable>(\w| )+)</td>" + _

"\s*.*\s*<t d class=td2 width=""65%"">( <b>){0,1}(?<val ue>.+)(</b>){0,1}</td>"

Dim Pattern2 As String = "<td class=td1
width=""35%"">( <b>){0,1}(?<var iable>(\w| )+)</td>" + _

"\s*.*\s*<t d class=td2
width=""65%"">( <b>){0,1}((?<va lue>.+))(</b>){0,1}</td>" ' extra parenthesis
don't help

rgx = New Regex(Pattern)

tbxPattern.Text = Pattern

Dim m As Match, g As Group

For Each m In rgx.Matches(tbx Input.Text)

g = m.Groups("varia ble")

lstbxKeys.Items .Add(g.Value)

g = m.Groups("value ")

lstbxValues.Ite ms.Add(g.Value)

Next

The data looks like this (below). It works fine for all cases except the
first (the "Celular" data) where the value is picked up as
"123-abc-5678</b>". I want, and I think it should be, "123-abc-5678". I
can't understand why the "</b>" is included in the value. Doesn't my
pattern clearly show that the value is a string of one or more characters,
terminated by, optionally, "</b>" followed by "</td>".
Yes, but remember that regexes are 'greedy' by default - they always
capture as many characters as they can. Thus when given a choice
between:

value: 123-abc-5678</b>
optional </b>: no

and

value: 123-abc-5678
optional </b>: yes

since the 'value' match happens first, and it can legitimately capture
everything including the </b>, it does so.
Is there a
straightforward way to tell it to not include the "</b>" in the value?


How about, instead of value capturing one or more of any character with
..+

you instead capture one or more characters that aren't < with

[^<]+

Also, there are flags you can put in to make expressions non-greedy,
but I don't think that will work in this situation.

BUT

I would *urge* you to stop trying to parse HTML with regex, and
instead run (don't walk) to
<http://smourier.blogsp ot.com/2005/05/net-html-agility-pack-how-to-use.html>,
and from there download HtmlAgilityPack , which is an absolutely
invaluable library that converts (even malformed) HTML into a nice XML
document tree. It makes doing HTML parsing a hundred times more easy
than trying to use regex.

--
Larry Lard
Replies to group please

Jun 13 '06 #2
Thank you very much Larry. It finally occurred to me that there had to be
some way to take advantage of the fact that the string I am after does not
contain "<", but the only solution I could think of was very ugly. Your
suggestion is much, much better. And thank you for making me aware of the
HtmlAgilityPack , I will be looking into it.

Thanks, Bob

"Larry Lard" <la*******@hotm ail.com> wrote in message
news:11******** *************@u 72g2000cwu.goog legroups.com...

eBob.com wrote:
I am using regular expressions and a particular feature called "capture"
(I
think) to suck some information out of some html. I could have never
come
up with this myself but Balena has an example which is very similar to
this.
The guts of the program is ...

Dim i As Integer
Dim rgx As Regex

Dim Pattern As String = "<td class=td1
width=""35%"">( <b>){0,1}(?<var iable>(\w| )+)</td>" + _

"\s*.*\s*<t d class=td2
width=""65%"">( <b>){0,1}(?<val ue>.+)(</b>){0,1}</td>"

Dim Pattern2 As String = "<td class=td1
width=""35%"">( <b>){0,1}(?<var iable>(\w| )+)</td>" + _

"\s*.*\s*<t d class=td2
width=""65%"">( <b>){0,1}((?<va lue>.+))(</b>){0,1}</td>" ' extra
parenthesis
don't help

rgx = New Regex(Pattern)

tbxPattern.Text = Pattern

Dim m As Match, g As Group

For Each m In rgx.Matches(tbx Input.Text)

g = m.Groups("varia ble")

lstbxKeys.Items .Add(g.Value)

g = m.Groups("value ")

lstbxValues.Ite ms.Add(g.Value)

Next

The data looks like this (below). It works fine for all cases except the
first (the "Celular" data) where the value is picked up as
"123-abc-5678</b>". I want, and I think it should be, "123-abc-5678".
I
can't understand why the "</b>" is included in the value. Doesn't my
pattern clearly show that the value is a string of one or more
characters,
terminated by, optionally, "</b>" followed by "</td>".


Yes, but remember that regexes are 'greedy' by default - they always
capture as many characters as they can. Thus when given a choice
between:

value: 123-abc-5678</b>
optional </b>: no

and

value: 123-abc-5678
optional </b>: yes

since the 'value' match happens first, and it can legitimately capture
everything including the </b>, it does so.
Is there a
straightforward way to tell it to not include the "</b>" in the value?


How about, instead of value capturing one or more of any character with
.+

you instead capture one or more characters that aren't < with

[^<]+

Also, there are flags you can put in to make expressions non-greedy,
but I don't think that will work in this situation.

BUT

I would *urge* you to stop trying to parse HTML with regex, and
instead run (don't walk) to
<http://smourier.blogsp ot.com/2005/05/net-html-agility-pack-how-to-use.html>,
and from there download HtmlAgilityPack , which is an absolutely
invaluable library that converts (even malformed) HTML into a nice XML
document tree. It makes doing HTML parsing a hundred times more easy
than trying to use regex.

--
Larry Lard
Replies to group please

Jun 13 '06 #3

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

Similar topics

1
2703
by: LuKrOz | last post by:
Someone could tell me how can I get the same result substituting ereg with preg_match and ereg_replace with preg_replace. $result = ereg("<\>(.+)<\>",$this->buffer,$token); $this->buffer = ereg_replace("<\>.+<\>","<>",$this->buffer) ; Thanks.
6
3173
by: Scott Zuyderduyn | last post by:
Hi all, I've started planning a software project that utilizes XML to store a lot of different input from the user. Regular expressions comprise a portion of this input. I could store a regex as a plain string ("^(\w+)\s+(\d+)$"), but this assumes POSIX-style regular expressions, and the software requirements can't neccessarily assume this. Has anyone ever created or seen an XML spec of a regular expression? For example, something...
4
3229
by: Neri | last post by:
Some document processing program I write has to deal with documents that have headers and footers that are unnecessary for the main processing part. Therefore, I'm using a regular expression to go over each document, find out if it contains a header and/or a footer and extract only the main content part. The headers and the footers have no specific format and I have to detect and remove them using a list of strings that may appear as...
2
1596
by: news.microsoft.com | last post by:
I need help design a reg exp. I am parsing an html file to get the input values, here is one example <input VALUE="Staff Writer" size=60 type="text" name="author"> Can I grab the value "Staff Writer" if name = "author"? is it possible using regexp? Thanks
2
2057
by: norton | last post by:
Hi, I am learning Regular Expression and currently i am trying to capture information from web page. I wrote the following code to capture the ID as well as the Title Dim regex = New Regex( _ "viewtopic.php\?t=(?<ID>\d+)""\sclass=""topictitle"">(?<Title>.*)\</a>", _ RegexOptions.IgnoreCase _
3
2550
by: Chris | last post by:
Hi everyone, I'm trying to parse through the contents of some text files with regular expressions, but am new to regular expressions and how to use them in VB.net. I'm pretty sure that the regular expressions are correct as I got them from regexlib.com and tested them in the Regulator and Expresso. The problem is I tested this function with a file that contains a string
14
2271
by: Chris | last post by:
I need a pattern that matches a string that has the same number of '(' as ')': findall( compile('...'), '42^((2x+2)sin(x)) + (log(2)/log(5))' ) = Can anybody help me out? Thanks for any help!
3
1164
by: Gayatri | last post by:
what does the flwg rule mean in regular expression: (.*) -- Regards, Gayatri M. Khorate.
3
2756
by: Zeba | last post by:
Hi guys, I need some help regarding regular expressions. Consider the following statement : System.Text.RegularExpressions.Match match = System.Text.RegularExpressions.Regex.Match(requestPath, "(*?\ \.ashx)"); (where requestPath is a string)
0
9522
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9336
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9948
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9902
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9765
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6603
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3866
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3446
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2738
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.