Emacsã®æ£è¦è¡¨ç¾ãä¸æã使ãããªããªãã»ã»ã»ã
ãããç§ã使ãããªããªãããããªãï¼ Emacs ãæªããã ï¼ï¼
â¦â¦ã¨æã£ãã®ã§æ¹é ã§ãã
é ããããï¼ Emacs ã®æ£è¦è¡¨ç¾
Emacs ã®æ£è¦è¡¨ç¾ã¯ç¬ç¹ããã¦é ããããã§ããä»ã®ã«æ £ãã¦ãã¨å ¨ç¶ä½¿ãããªãã¾ããã
RegExp replace ï¼æ£è¦è¡¨ç¾ã§ã®ç½®æï¼ã®ã¨ããã
ã©ã®æåãæ£è¦è¡¨ç¾ã«å¼ã£ããã£ã¦ãããããããçµæã
Replaced 0 occurrences
ã®ä¸æãè¦ã¦ã¬ãã«ãªãããã¨ã«ãªãã¾ãã
Emacs æ¨æºæè¼ã® Re-Builder ã¨ããæ©è½ã使ã£ã¦ãæ£è¦è¡¨ç¾ãä¸è´ãããè¦ããã¨ã¯åºæ¥ã¾ããã»ã»ã»*1
ã¡ãã£ã¨ä¾ãè¦ã¦ã¿ã¾ãããã
使ããªã Re-Builder
<p name="lemon"> lemon </p> <p name="melon"> melon </p>
ããã® name="" ã®ä¸èº«ããããã fruits-lemon, fruits-melon ã«ç½®æãããã¨èãã¦ã¿ã¾ãããã
ã§ãM-x re-builder
㧠Re-Builder ãç«ã¡ä¸ãã¦ã
åºã¦ããä¸ã® *RE-Builder* ããã¯ã¹ã«å
¥åãã¦ç¢ºèªãã¾ãããã
ãªãã»ã©ãemacs ã§ã¯ \"\\([a-z]+on\\)\"
ã¨å
¥åããã°ãããã ãªã
ããã¶ãããã¯ã¹ã©ãã·ã¥å¤ããã ãªãã¨ç解ãã¦ãã㧠replace ãã¦ã¿ããã¨ãã¦ã¿ã¾ãã
ãããç¾å®ã¯éæ
ï¼ï¼
ããã«ã¯ Replaced 0 occurrences ã®æåãï¼ï¼
ãRe-Builderããã®åã¤ãï¼ï¼ã
ã¨ç´çãªå°å¥³ã£ã½ãè¨èãå«ã³ãããªãã¾ãã
ã¡ãªã¿ã«æ£ããã¯ã "\([a-z]+on\)"
â "fruits-\1"
ã§ç½®æã§ãã¾ãã
ããã¨ãµã¤ãã§ãã
ããã©ããããå¼ã£ããããã¤ã³ããªæ¹è¡ã³ã¼ã
æ£è¦è¡¨ç¾ã®æ¹è¡ã³ã¼ãã¨ããã°ãå½ç¶ãããã¯ã¹ã©ãã·ã¥nã\n
ã§ãã
Emacs ã¯ããã§ã¯ããã¾ãããC-q C-j
ã§ããé ããããã§ãã
ã£ã¦ããç§ã¯ C-TAB 㯠next buffer ã¸ã®ç§»åãC-q 㯠previous buffer ã¸ã®ç§»åã«ãã¼ãã¤ã³ããã¦ãã®ã§ã
C-q æ¼ããã¨ããã«éããããã¡ã¼ï¼ã¿ãï¼ã«é£ã°ããã¡ããã®ã§ä½¿ãã¾ããï¼ï¼
公式の正規表現の案内のページã«ã¯
You can enter a newline character using
âC-oâ
,âC-q C-jâ
, orâC-q 012 RETâ
.
ã¨æ¸ããã¦ãã¾ãããç§ã®ç°å¢ã§ã¯ C-o ã§ãã¡ã§ãããï¼ãªãããã¼ãã¤ã³ãæ¯ã£ã¦ãã£ãï¼
解決çï¼ : foreign-regexp ã使ãã
詳ããã¯ãã¡ãã : Emacs: re-builder + foreign-regexp.elでたのしい正規表現 | 葉月夜堂
ãã ããããã§ãæ¸ããã¦ãã¾ããã
Emacs ã®ãã¼ã¸ã§ã³ã 24.4 ã 24.5 ã§ã¯
Wrong type argument: commandp, ad-Orig-foreign-regexp/query-replace
ã¨ã¨ã©ã¼ãåºã¦ä½¿ãã¾ããã
ç§ã®Emacsã¯ææ°ã® 24.5 ã«ãã¦ããã®ã§åãã¨ã©ã¼ãåºã¦ä½¿ãã¾ããã§ããã
Perl, Ruby, Python, JavaScript ã¨ãã£ãããããªè¨èªã®æ£è¦è¡¨ç¾å½¢å¼ã«å¯¾å¿ãã¦ãã¦ãè¯ãããã ã£ããã§ããã©ãã
å¥ãªæ¹æ³ãèãã¾ãããã
解決çï¼ï¼æ¬å½ï¼ : visual-regexp-steroids ã使ãã
åè : visual-regexp-steroids.el : 【正規表現革命】isearchや置換でPerl/Pythonの正規表現を使おうぜ!
å°å ¥ãå°ãç¹æ®ãªã®ã§æ°ãã¤ãã¾ãããã
ã¾ã visual-regexp-steroids ã®ããã±ã¼ã¸ãã¤ã³ã¹ãã¼ã«ãã¾ãã
ãã®ã¨ãã visual-regexp ã®ããã±ã¼ã¸ãã¤ã³ã¹ãã¼ã«ãããã¨æãã¾ããã
ãã¡ãã大äºã§ãã®ã§ãemacs ã®ãã©ã°ã¤ã³ãç½®ãã¦ãããã©ã«ãã«ã©ã¡ããç½®ãã¦ãã ããã
ãããããpcre2el ã¨ããããã±ã¼ã¸ãã¤ã³ã¹ãã¼ã«ãã¦ããã¨ããã§ãããã
ããã¯ãPython ãã¤ã³ã¹ãã¼ã«ããã¦ããªãï¼PATHãéã£ã¦ãªãï¼ãã½ã³ã³ã§ visual-regexp ã使ãããã¨ãã«å¿
è¦ã«ãªãã¾ãã
ç§ã¯è¤æ°ãã½ã³ã³ã§Emacsã®è¨å®ã使ãã¾ããã¦ããã®ã§ããã¡ãã pcre2el ã¯ã¤ã³ã¹ãã¼ã«ãã¦ããã¾ããã
ãã¦ããã©ã°ã¤ã³ãEmacsãèªã¿åããã©ã«ãã¼ã«ç½®ãã¾ãããã
init.el ï¼Emacsã®è¨å®ãã¡ã¤ã«ï¼ã«ä»¥ä¸ã®ããã«æ¸ã足ãã¾ãã
(defvar emacs-root (eval-when-compile '( (require 'visual-regexp-steroids) ))) ;; ä¸è¨ã®ããã«æ¸ããªãã¨ãã³ã³ãã¤ã«æã« `regexp-string' ç±æ¥ã®è¦åãåã (setq vr/engine 'python) ; 'python, 'pcre2el, or 'emacs ;; python ã ã¤ã³ã¹ãã¼ã«ããã¦ãªãç°å¢ã§ã¯ãä¸ï¼è¡ãã³ã¡ã³ãã¢ã¦ããä¸ï¼è¡ãã³ã¡ã³ãè§£é¤ ;; (setq vr/engine 'pcre2el) ;; (require 'pcre2el) ;; multiple-cursors ( https://github.com/magnars/multiple-cursors.el ) ã使ã£ã¦ããå ´åã¯ä¸ï¼è¡ãã³ã¡ã³ãè§£é¤ ; (global-set-key (kbd "C-c m") 'vr/mc-mark) ;; æ®æ®µã® 'query-replace-regexp ã visual-regexp ã« (define-key global-map (kbd "C-x C-r") 'vr/query-replace)
ãµã ã㯠(require 'visual-regexp-steroids) ã ãã§ãã©ã°ã¤ã³ãèªã¿è¾¼ãã¨æãã¾ããã
~/.emacs.d/elpa/visual-regexp-steroids/visual-regexp-steroids.elc:Warning: reference to free variable `regexp-string' ~/.emacs.d/elpa/visual-regexp-steroids/visual-regexp-steroids.elc:Warning: reference to free variable `replace-string'
㨠Warning ãåãã¦ãã£ã¨ãããã®ã§ãä¸ã®ããã«è¨è¿°ãã¦ãããã¨ããªã¹ã¹ã¡ãã¾ãã
*追è¨(2015/9/12 22:00)ï¼ä¸è¨ã®ããã«æ¸ããã®ã§ããããããè¨è¿°ããã¨ãããã visual-regexp ãåããªããªããã¨ã«æ°ä»ããã®ã§ããã¦ãã ããã
ãµã¤ãã« (require 'visual-regexp-steroids)
ã§å¼ãã§ãã ããã
èµ·åã®ãã³ã« *Compile-Log* ã®ãããã¡ãéãã¦ã¡ãã£ã¨å«ã§ãããããããããããªãã§ãããã
visual-regexp-steroids ãå°å ¥ããã¨ãããªãï¼
ç½®æãããç®æããã¤ã©ã¤ããããããè¦ããã
æ£è¦è¡¨ç¾ã«ä¸è´ããé¨åããã¤ã©ã¤ãããã¾ãï¼
ç½®æå¾ã«ã©ããªããã表示ãã¦ããã¾ãï¼
ãã¨ã¯ãã¤ãéããy ã n ã ! ãæ¼ãã¦ç½®æããã ãã§ãã
è¤æ°è¡ã®ç½®æãç°¡åã«ã§ãã
ä¾ãã°ãã®ããã«ã
<p name="lemon" > lemon </p>
" 㨠> ãå¥ã®è¡ã«è¡ã£ã¦ãã¾ã£ã¦ãã®ã§é£çµããããã¨ãã¾ãããã
ãã®ã¨ãã¯ã¦ã£ã³ãã¦ä¸ã«åºã¦ããããã«ãC-c m
ãæ¼ãã¨è¤æ°è¡æ¤ç´¢ã«ãªãã¾ããã*2
ã¡ãã㨠\n
ã使ãã¾ãã®ã§ç°¡åã«ç½®æãããã¨ãã§ãã¾ãã
ãï¼ ããããã§ããï¼
Emacs ã®æ£è¦è¡¨ç¾ã®èéãããªãã«å°ã£ã¦ã人ããã² visual-regexp-steroids ã使ãã¾ãããã
ããç¡ãã®æ£è¦è¡¨ç¾ç½®æã¯ããããªããªãã¾ããï¼
ã¡ãªã¿ã«
ç§ã¯æ£è¦è¡¨ç¾ãç½®æã§ãã使ããªãã®ã§ãã£ã¦ãã¾ãããã
search ã§ãããã使ãããå ´åã¯ã
(global-set-key (kbd "C-S-s") 'vr/isearch-forward) ;; Ctrl + Shift + s ã§åæ¹æ¤ç´¢ (global-set-key (kbd "C-S-r") 'vr/isearch-backward) ;; Ctrl + Shift + r ã§å¾æ¹æ¤ç´¢ ;; ãã¼ãã¤ã³ãã¯ã好ã¿ã§â¦â¦
ãªã©ã¨æ¸ãã¦ããã° OK ã§ãã
*1:Re-Builderã«ã¤ã㦠: Emacs で正規表現を使うなら re-builder を使おう - higepon blog
*2:ãããã®ããã«ããããã®ã¡ãã¥ã¼ãåºã¦ããªãã¨ãã¯ãM-x vr/select-query-replace ã®ã®ã¡ã« python ã¨å ¥åããã¨ç´ããã¨æãã¾ã