Why won't this find work?

Started by methodic, July 20, 2006, 10:00:20 PM

Previous topic - Next topic

methodic

I have a webpage I am trying to parse... I slurp it with get-url, replace all whitespace with one space (newlines, tabs, multiple spaces, etc)... here is the part I am trying to grab: "
 
 Last Login:  7/20/2006
 </td>"



here is my find: (find "
 Last Login:&nbsp; (.*)
" txt 0)



and it returns up to 7/20/2006, but it doesn't stop at the
, it keeps going past it to the end of the file... I am using multiple finds on this page, and every of them return fine, are the forward slashes in the date screwing up find?



thanks.

methodic

#1
Nevermind, I see what I did wrong, sorry. :)



I guess that's a *feature* of find. ;)

cormullion

#2
I've found regex useful when working with this stuff:


(set 'txt "<br> <br> Last Login:&nbsp; 7/20/2006<br> </td>")
(regex "<br> Last Login:&nbsp; (.*)<br>" txt 0)
;-> ("<br> Last Login:&nbsp; 7/20/2006<br>" 5 36 "7/20/2006" 28 9)


which tells you you want the $1 value rather than the $0:


(find "<br> Last Login:&nbsp; (.*)<br>" txt 0)
;-> 5
(println $0)
;-><br> Last Login:&nbsp; 7/20/2006<br>
(println $1)
;->7/20/2006


I'm still struggling with this myself...! Yellow RegExp-Do belt worn with pride... ;-)

Lutz

#3
In both cases $0 and $1 show the same: $0 the whole area covered by the pattern and $1 the parenthesized subpattern:

> (regex "<br> Last Login:&nbsp; (.*)<br>" txt 0)
("<br> Last Login:&nbsp; 7/20/2006<br>" 5 36 "7/20/2006" 28 9)

> $0
"<br> Last Login:&nbsp; 7/20/2006<br>"
> $1
"7/20/2006"

> (find "<br> Last Login:&nbsp; (.*)<br>" txt 0)
5

> $0
"<br> Last Login:&nbsp; 7/20/2006<br>"
> $1
"7/20/2006"
>


In the list returned by regex imagine 3 members are always grouped together: string, offset and length.



The first group then corresponds to $0 the next to $1 etc.



Lutz

Lutz

#4
to Methodic:



the .* operator will always grab as much as it can and still satify the pattern. You can use the option 512 to invert greediness or put an ? after the star as in .*?

> (find "a.*c" "abbbbcbbbcd" 0)
0
> $0
"abbbbcbbbc"

> (find "a.*c" "abbbbcbbbcd" 512)
0
> $0
"abbbbc"
>

> (find "a.*?c" "abbbbcbbbcd" 0)
0
> $0
"abbbbc"


and here the same with parenthesized subexpressions to isolate the subpattern:



> (find "a(.*)c" "abbbbcbbbcd" 0)
0
> $1
"bbbbcbbb"

> (find "a(.*?)c" "abbbbcbbbcd" 0)
0
> $1
"bbbb"


Lutz

methodic

#5
Ah, thats what I was looking for, the (.*?)



thanks so much!