help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Shortening words with multiple rules


From: uzibalqa
Subject: Re: Shortening words with multiple rules
Date: Tue, 16 Aug 2022 19:06:12 +0000

------- Original Message -------
On Tuesday, August 16th, 2022 at 10:45 AM, Arash Esbati <arash@gnu.org> wrote:


> uzibalqa uzibalqa@proton.me writes:
>
> > It does help a lot. How would a match at the end of word (matching
> > "ley" "ily" "ly") look like, with your scheme?
>
>
> Maybe something like this:
>
> (defun shorten-word ()
> "Shortens a word according to specific rules."
> (interactive)
> (let* ((bounds (bounds-of-thing-at-point 'word))
> (s (car bounds))
> (case-fold-search nil)
> (p (point-marker)))
> (when s
> (goto-char s)
> (cond ((looking-at (regexp-opt '("cog" "col" "com" "con"
> "cor" "cum" "coun")
> "\\<\\("))
> (replace-match "k"))
> ((looking-at (concat "[[:alpha:]]*?"
> "\\("
> (regexp-opt '("ley" "ily" "ly"))
> "\\)\\>"))
>
> (replace-match "X" nil nil nil 1))
> (t nil))
> (goto-char p))
> (set-marker p nil)))
>
> Best, Arash

Works very well Arash.

Another task would be to match character sequences in the middle of a word.

I have been using the following regex to match "ple" inside a word and replacing
it with the letter "p", allowing punctuation.

(replace-regexp-in-string
   "\[[:alpha:]]+[-]?\\)ple\\([[[:alpha:]]+[[:punct:]]?\\)" "\\1p\\2" word)

How would this translate to your function?




reply via email to

[Prev in Thread] Current Thread [Next in Thread]