[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: string searching and saving results to a variable
From: |
ken |
Subject: |
Re: string searching and saving results to a variable |
Date: |
Wed, 16 Feb 2011 01:55:56 -0500 |
User-agent: |
Thunderbird 2.0.0.24 (X11/20101213) |
On 02/15/2011 06:33 PM Perry Smith wrote:
> On Feb 15, 2011, at 5:15 PM, ken wrote:
>
>> It's time again to write an elisp function!!
>>
>> One thing it needs to do a couple of times is save a string to a
>> variable. The string to save will be an html header, like:
>>
>> <h3>Section 4</h3>
>>
>> but it could be multiple lines like this
>>
>> <h3 class="newest-chapter-section-type" align="center">On
>> the origins of elisp confusion</h3>
>>
>> It could even be three or four lines long. Also, the line(s) could be
>> indented and so have unwanted white space in the first several columns.
>>
>> Assuming the point is somewhere on that line or one of those lines, we do:
>
> To be clean, first declare some variables
>
> (defvar ....)
>
>> (end-of-line) ; to preclude the point being at the far left.
>> ; find the start of the string:
>> (re-search-backward "<h1\\|<h2\\|<h3\\|<h4\\|<h5" nil t)
>> ;;somehow mark this as the beginning of the string???
>
> save the current point with:
>
> (setq beg (point))
>
>> ;find the endpoint of the string:
>> (re-search-forward "</h1>\\|</h2>\\|</h3>\\|</h4>\\|</h5>" nil t)
>>
>> ;;save string to a variable to do other things with... how???
>
> (setq dog (buffer-substring beg (point)))
>
> dog now has the string
>
Thanks!!
I'm guessing it could then be done more elispishly as
(setq dog (buffer-substring
(re-search-backward "<h1\\|<h2\\|<h3\\|<h4\\|<h5" nil t)
(re-search-forward "</h1>\\|</h2>\\|</h3>\\|</h4>\\|</h5>" nil t)))