regex help 
Author Message
 regex help

Hi

the file passed to the proc below looks like ...<some.url.here>Next 3
rows ...

I am trying to return the url for a given tag as below but it is not
capturing any results even there is some, I wounder if I got the regex
wrong.

proc hasNext {somefile} {
    set g [open "$somefile" "r"];
    set pat1 {<(.*)>.*Next.*rows};
    if {[regexp $pat1 $g newurl]} {
        return $newurl;}}

thanks



Mon, 29 Sep 2008 22:48:42 GMT  
 regex help

Quote:

> Hi

> the file passed to the proc below looks like ...<some.url.here>Next 3
> rows ...

> I am trying to return the url for a given tag as below but it is not
> capturing any results even there is some, I wounder if I got the regex
> wrong.

> proc hasNext {somefile} {
>     set g [open "$somefile" "r"];
>     set pat1 {<(.*)>.*Next.*rows};
>     if {[regexp $pat1 $g newurl]} {
>    return $newurl;}}

> thanks

g is just a file handle, you need to read the data
before applying your regex, also the first variable
regexp fills has the entire match, if you want just
your submatch you need another variable, try this
and see if it gets you closer to what you want.
(but also note the parsing html/xml with regex has a
*lot* of potential pitfalls to bite you with later)

proc hasNext {somefile} {
     set fd [open "$somefile" "r"];
     set txt [read $fd]
     close $fd
     set pat1 {<(.*)>.*Next.*rows};
     if {[regexp $pat1 $txt all newurl]} {
        return $newurl
     }

Quote:
}

Bruce


Mon, 29 Sep 2008 23:09:51 GMT  
 regex help
You are missing the variable for the whole match, i.e. should be
regexp $pat1 $x dummy newurl
which returns 1 and sets new url to some.url.here
dummy is set to <some.url.here>Next 3 rows

BTW a good rule to follow with regexps is to avoid using .* if at all
possible. It is much safer to be specific, in this case
   set pat1 {<([^>]+)>\s*Next\s+\d+\s+rows}
.* is like using a chainsaw



Mon, 29 Sep 2008 23:17:25 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. RegEx help need!

2. Need regex help (or bug in match)

3. Regex Help Please

4. regex help please please !

5. regex help

6. Regex help

7. Regex help

8. Regex Help

9. regex help needed !

10. grep regex to ruby regex...

11. Regex++ or other Regex lib for Fortran?

12. Python regex / libc regex interactions

 

 
Powered by phpBB® Forum Software