문자열 치환, 정규표현식, emacs

rainmon의 이미지

,a,:ls_foo,c, bar, d,e 소스에 이런 문자열을 다음과 같이 치환하려고 합니다.

,a
,:ls_foo
,c
,bar
,d
,e
변경전(검색대상)과 변경후(바꿔질 형태)를 정규표현식으로 어떻게 표현하면 될까요? 편집기로는 emacs를 쓰고있지만 정규표현식은 비슷할꺼라 생각합니다.
cinsk의 이미지

M-x query-replace-regexp <RET>
,[ ]*\([^,]*\)<RET>
C-x j<RET>

여기서 C-x j는 말 그대로 control x를 누른 다음 j를 치는 것을 말합니다.
cinsk의 이미지

한 가지 더, (Perl을 모르는 상황에서 이런 말을 한다는 것이 여러분들의 화를 돋울 지는 모르겠지만.. :twisted:) Emacs만큼 강력한 text 치환을 지원하는 tool도 없습니다.

예를 들어보면:
x는 y로, y는 x로 바꾸는 예제.
원본:

x y
y x
y y
x x

명령:
M-x replace-regexp <RET> \(x\|y\) <RET>
\,(if (string= "y" \1) "x" "y"))<RET>

결과:

y x
x y
x x
y y


원본:
모든 줄 뒤에 ABC000XX 꼴로 증가 번호를 단다.
Hello, world
GNU Emacs has owesome text substitution capability.
I love Emacs!

명령:
M-x replace-regexp <RET> ^.\{0,72\}$ <RET>
  \,(format "%-72sABC%05d" \& \#) <RET>

결과:

Hello, world                                                            ABC00000
GNU Emacs has owesome text substitution capability.                     ABC00001
I love Emacs!                                                           ABC00002
rainmon의 이미지

cinsk wrote:
M-x query-replace-regexp <RET>
,[ ]*\([^,]*\)<RET>
C-x j<RET>

여기서 C-x j는 말 그대로 control x를 누른 다음 j를 치는 것을 말합니다.

이정도 알려주시면 알아서 해결해야 할텐데 진행중 전혀 다른 결과가..
말씀대로 해보니 제 emacs에서는 C-x j를 누르면
미니버퍼에 Jump to register: 라고 뜨네요.

C-x j를 누르기전까지의 미니버퍼는 아래와 같은 대기상태였습니다.

Query replace regexp ,[ ]*\([^,]*\) with:

여기서 with: 에 C-x j를 눌렀습니다.

cinsk의 이미지

rainmon wrote:

이정도 알려주시면 알아서 해결해야 할텐데 진행중 전혀 다른 결과가..
말씀대로 해보니 제 emacs에서는 C-x j를 누르면
미니버퍼에 Jump to register: 라고 뜨네요.

Ooops. 실수네요.. C-x j가 아니라 C-q C-j입니다.

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.