For Programmers: Free Programming Magazines  


Home > Archive > Tcl > April 2005 > regexp and multiple matches









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author regexp and multiple matches
Garry Horoupian

2005-04-25, 8:58 pm

Hello,

I was wondering if anyone could tell me how to capture multiple matches of a
regexp expression into a list.

For example, if I wanted to parse an html row for it's cell values,
regardless of how many cells there are:

<tr><td>Timer values</td><td>1</td><td>2</td><td>3</td>........</tr>



Thanks in advance


SM Ryan

2005-04-25, 8:58 pm

"Garry Horoupian" <ghoroupi@cisco.com> wrote:
# Hello,
#
# I was wondering if anyone could tell me how to capture multiple matches of a
# regexp expression into a list.
#
# For example, if I wanted to parse an html row for it's cell values,
# regardless of how many cells there are:
#
# <tr><td>Timer values</td><td>1</td><td>2</td><td>3</td>........</tr>

tclsh <<':eof'
set string {<tr><td>Timer values</td><td>1</td><td>2</td><td>3</td>........</tr>}
puts [regexp -inline -all {<td>.*?</td>} $string]
:eof

{<td>Timer values</td>} <td>1</td> <td>2</td> <td>3</td>


tclsh <<':eof'
set string {<tr><td>Timer values</td><td>1</td><td>2</td><td>3</td>........</tr>}
puts [regexp -inline -all {<td>(.*?)</td>} $string]
:eof

{<td>Timer values</td>} {Timer values} <td>1</td> 1 <td>2</td> 2 <td>3</td> 3


tclsh <<':eof'
set string {<tr><td>Timer values</td><td>1</td><td>2</td><td>3</td>........</tr>}
set L {}
foreach {- cell} [regexp -inline -all {<td>(.*?)</td>} $string] {lappend L $cell}
puts $L
:eof

{Timer values} 1 2 3


Note also that HTML is a type 2 language and cannot be properly parsed with just a
regular expression. For example, if a table is nested within a <td>.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
You hate people.
But I love gatherings. Isn't it ironic.
davidhbigelow@simplifiedlogic.com

2005-04-25, 8:58 pm


Garry Horoupian wrote:
> Hello,
>
> I was wondering if anyone could tell me how to capture multiple

matches of a
> regexp expression into a list.
>
> For example, if I wanted to parse an html row for it's cell values,
> regardless of how many cells there are:
>
> <tr><td>Timer values</td><td>1</td><td>2</td><td>3</td>........</tr>
>
>
>
> Thanks in advance


if memory serves:

regexp -all -inline {<td>[A-Za-z0-9\ ]+</td>} $data

Might get you close...

Dave

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com