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: "<br> <br> Last Login: 7/20/2006<br> </td>"
here is my find: (find "<br> Last Login: (.*)<br>" txt 0)
and it returns up to 7/20/2006, but it doesn't stop at the <br>, 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.
Why won't this find work?
-
- Posts: 2038
- Joined: Tue Nov 29, 2005 8:28 pm
- Location: latiitude 50N longitude 3W
- Contact:
I've found <b>regex</b> useful when working with this stuff:
which tells you you want the $1 value rather than the $0:
I'm still struggling with this myself...! Yellow RegExp-Do belt worn with pride... ;-)
Code: Select all
(set 'txt "<br> <br> Last Login: 7/20/2006<br> </td>")
(regex "<br> Last Login: (.*)<br>" txt 0)
;-> ("<br> Last Login: 7/20/2006<br>" 5 36 "7/20/2006" 28 9)
Code: Select all
(find "<br> Last Login: (.*)<br>" txt 0)
;-> 5
(println $0)
;-><br> Last Login: 7/20/2006<br>
(println $1)
;->7/20/2006
In both cases $0 and $1 show the same: $0 the whole area covered by the pattern and $1 the parenthesized subpattern:
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
Code: Select all
> (regex "<br> Last Login: (.*)<br>" txt 0)
("<br> Last Login: 7/20/2006<br>" 5 36 "7/20/2006" 28 9)
> $0
"<br> Last Login: 7/20/2006<br>"
> $1
"7/20/2006"
> (find "<br> Last Login: (.*)<br>" txt 0)
5
> $0
"<br> Last Login: 7/20/2006<br>"
> $1
"7/20/2006"
>
The first group then corresponds to $0 the next to $1 etc.
Lutz
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 .*?
and here the same with parenthesized subexpressions to isolate the subpattern:
Lutz
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 .*?
Code: Select all
> (find "a.*c" "abbbbcbbbcd" 0)
0
> $0
"abbbbcbbbc"
> (find "a.*c" "abbbbcbbbcd" 512)
0
> $0
"abbbbc"
>
> (find "a.*?c" "abbbbcbbbcd" 0)
0
> $0
"abbbbc"
Code: Select all
> (find "a(.*)c" "abbbbcbbbcd" 0)
0
> $1
"bbbbcbbb"
> (find "a(.*?)c" "abbbbcbbbcd" 0)
0
> $1
"bbbb"