Regex Searching in Emacs

Posted by Inaimathi on Stack Overflow See other posts from Stack Overflow or by Inaimathi
Published on 2010-05-14T14:21:02Z Indexed on 2010/05/14 14:24 UTC
Read the original article Hit count: 333

Filed under:
|
|
|

I'm trying to write some Elisp code to format a bunch of legacy files.

The idea is that if a file contains a section like "<meta name=\"keywords\" content=\"\\(.*?\\)\" />", then I want to insert a section that contains existing keywords. If that section is not found, I want to insert my own default keywords into the same section.

I've got the following function:

(defun get-keywords ()
      (re-search-forward "<meta name=\"keywords\" content=\"\\(.*?\\)\" />")
      (goto-char 0) ;The section I'm inserting will be at the beginning of the file
      (or (march-string 1)
          "Rubber duckies and cute ponies")) ;;or whatever the default keywords are

When the function fails to find its target, it returns Search failed: "[regex here]" and prevents the rest of evaluation. Is there a way to have it return the default string, and ignore the error?

© Stack Overflow or respective owner

Related posts about emacs

Related posts about regex