[완료] 정규 표현식으로 이런 것도 가능할까요?

agrajag의 이미지

안녕하세요, 갑자기 펄로 뭔가 스크립트를 짤 일이 생겨 버린 초짜입니다. 프로그래밍은 전혀 모르고 살았는데 참 난감하네요.

이런 문자열이 있습니다.
m:\abc\bcd\defg\efgh.c@@\main\br1\bra2\bra3\5

여기서 맨 앞의 m:\abc를 떼내고 맨 끝의 숫자를 1을 빼서 치환할 수 있을까요? 원하는 결과는 이렇습니다.
\bcd\defg\efgh.c@@\main\br1\bra2\bra3\4

여기서 \bcd\defg\ 등등은 디렉토리라서 더 길어질 수도 있고, 마찬가지로 @@ 뒤쪽의 \로 구분된 문자들도 더 많아질 수 있습니다.

정규표현식으로 어떻게 하다 보면 될 것 같긴 한데 뭔가 복잡해서 잘 모르겠습니다. 조그만 힌트라도 주시면 감사하겠습니다.

ironiris의 이미지

정규표현식을 쓸 일도 없이 펄로 문자열을 조작하면 되겠네요.
PHP로 생각한다면 \ 로 문자열을 split 시킨후(배열로 나오겠지요?) [0]번을 지워버리고 마지막 배열의 원소는 숫자가 있을테니 그것을 -1 한후
다시 \ 로 join 하면 될것 같습니다.

agrajag의 이미지

감사합니다. 알려주신 그대로 split 했다가 join해서 깔끔하게 성공했습니다.
정말 간단하네요. 이걸 정규표현식으로 하겠다고 생각하고 있었으니.. -_-

potatoid의 이미지

#!/usr/bin/env perl
 
use strict;
 
print "Testing of Regular expression\n";
 
my $str = 'm:\abc\bcd\defg\efgh.c@@\main\br1\bra2\bra3\5';
 
$str =~ s/m:\\abc(.*\\)(\d*)/$1$2/g;
 
my $val = $2 - 1;
 
print "$1$val\n";
 
exit 0;

이렇게 하면 간단하게 해결 될 것 같은데요. :-)

> Sorrow is better than laughter, because a sad face is good for the heart.

--------------------------------------------------------------------------
Sorrow is better than laughter, because a sad face is good for the heart.

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.