Regular expressions: Difference between revisions

Content added Content deleted
(Emacs Lisp: Simplify solution)
Line 854: Line 854:


=={{header|Emacs Lisp}}==
=={{header|Emacs Lisp}}==
<lang Emacs Lisp>
<lang Lisp>(let ((string "I am a string"))
(defun match (word str)
(when (string-match-p "string$" string)
(setq pos (string-match word str) )
(message "Ends with 'string'"))
(message "%s" (replace-regexp-in-string " a " " another " string)))</lang>
(if pos
(progn
(insert (format "%s found at position %d in: %s\n" word pos str) )
(setq regex (format "^.+%s" word) )
(setq str (replace-regexp-in-string regex (format "left %s" word) str) )
(setq regex (format "%s.+$" word) )
(setq str (replace-regexp-in-string regex (format "%s right" word) str) )
(insert (format "result: %s\n" str) ))
(insert (format "%s not found in: %s\n" word str) )))


{{out}}
(setq str1 "before center after" str2 "before centre after")


Ends with 'string'
(progn
I am another string
(match "center" str1)
(insert "\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>


=={{header|Erlang}}==
=={{header|Erlang}}==