Regular expressions: Difference between revisions

Line 547:
=={{header|Emacs Lisp}}==
<lang Emacs Lisp>
(defun match (str word str)
(ifsetq pos (string-match word str) )
(if (prognpos
(progn
(insert (format "%s found in: %s\n" word str) )
(insert (format "%s found at position (setq%d regexin: (format "^.+%s\n" word pos str) )
(setq str (replace-regexp-in-string regex (format "left ^.+%s" word) str) )
(setq str (setqreplace-regexp-in-string regex (format "left %s.+$" word) str) )
(setq str (replace-regexp-in-string regex (format "%s right.+$" word) str) )
(setq str (replace-regexp-in-string (insertregex (format "result: %s\n right" word) str) ))
(insert (format "%s not found inresult: %s\n" word str) )))
(insert (format "%s not found in: %s\n" word str) )))
 
(setq str1 "before center after" str2 "before centre after")
 
(progn
(match str1 "center" str1)
(match str2insert "center\n"))
(match "center" str2) )
</lang>
<b>Output:</b>
<pre>
center found at position 7 in: before center after
result: left center right
 
center not found in: before centre after
</pre>
678

edits