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}(?<variable>(\w| )+)</td>" + _
"\s*.*\s*<td class=td2 width=""65%"">(<b>){0,1}(?<value>.+)(</b>){0,1}</td>"
Dim Pattern2 As String = "<td class=td1
width=""35%"">(<b>){0,1}(?<variable>(\w| )+)</td>" + _
"\s*.*\s*<td class=td2
width=""65%"">(<b>){0,1}((?<value>.+))(</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(tbxInput.Text)
g = m.Groups("variable")
lstbxKeys.Items.Add(g.Value)
g = m.Groups("value")
lstbxValues.Items.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%">Edad</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%">Altura</td>
<td width=1><img src="../img/p.gif" width=1 height=1></td>
<td class=td2 width="65%">1.70 mts.</td> 2 1357
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}(?<variable>(\w| )+)</td>" + _
"\s*.*\s*<td class=td2 width=""65%"">(<b>){0,1}(?<value>.+)(</b>){0,1}</td>"
Dim Pattern2 As String = "<td class=td1 width=""35%"">(<b>){0,1}(?<variable>(\w| )+)</td>" + _
"\s*.*\s*<td class=td2 width=""65%"">(<b>){0,1}((?<value>.+))(</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(tbxInput.Text)
g = m.Groups("variable")
lstbxKeys.Items.Add(g.Value)
g = m.Groups("value")
lstbxValues.Items.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.blogspot.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
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*******@hotmail.com> wrote in message
news:11*********************@u72g2000cwu.googlegro ups.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}(?<variable>(\w| )+)</td>" + _
"\s*.*\s*<td class=td2 width=""65%"">(<b>){0,1}(?<value>.+)(</b>){0,1}</td>"
Dim Pattern2 As String = "<td class=td1 width=""35%"">(<b>){0,1}(?<variable>(\w| )+)</td>" + _
"\s*.*\s*<td class=td2 width=""65%"">(<b>){0,1}((?<value>.+))(</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(tbxInput.Text)
g = m.Groups("variable")
lstbxKeys.Items.Add(g.Value)
g = m.Groups("value")
lstbxValues.Items.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.blogspot.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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 =...
|
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...
|
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...
|
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...
|
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( _...
| |
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...
|
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!
|
by: Gayatri |
last post by:
what does the flwg rule mean in regular expression:
(.*)
--
Regards,
Gayatri M. Khorate.
|
by: Zeba |
last post by:
Hi guys,
I need some help regarding regular expressions. Consider the following
statement :
System.Text.RegularExpressions.Match match =...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
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,...
| |
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...
|
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,...
|
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...
|
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,...
|
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: 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 ...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |