The function you are looking for is
search(). This function will search starting at the cursor position, and when a match is found its line number will be returned. If no match is found, 0 is returned. The
'magic' options are used on the search pattern. Note that if you want to choose where the search starts at, you can use the
cursor() or the
setpos() function to set the cursor position, and the
getcurpos() function to save the cursor location.
Here is an example of it in action:
function! SearchInRange(pattern, start_line, end_line)
" Save cursor position.
let save_cursor = getcurpos()
" Set cursor position to beginning of file.
call cursor(a:start_line, 0)
" Search for the string 'hello' with a flag c. The c flag means that a
" match at the cursor position will be accepted.
let search_result = search(pattern, "c", a:end_line)
" Set the cursor back at the saved position. The setpos function was
" used here because the return value of getcurpos can be used directly
" with it, unlike the cursor function.
call setpos('.', save_cursor)
" If the search function didn't find the pattern, it will have
" returned 0, thus it wasn't found. Any other number means that an instance
" has been found.
return search_result ? 1 : 0
For more on the things mentioned in this answer, see the following help topics: