'Regularexpression'에 해당되는 글 1건

  1. 2008.07.17 펄에서 특정 패턴 확인하기



펄에서 스트링에 대해서 특정 패턴이 나타나는지 확인하고 싶을 때 있다.  이때는 펄의 RE를 사용해 보자. 간단한 예가 밑에 있어서 사용.


     if ($r =~ m/error getting credentials: Ticket expired/) {
        print "** $HOST server 리스타트 실패되었습니다. 커보러스 티켓이 expired되었습니다. kinit -f {계정} 해주시면 사용하실수 있습니다. \n\n"
    } else {
        print ("$HOST server 리스타트가 완료되었습니다.\n");




Section 1: Basic matching and substitution

Declare a local variable called $mystring.

my $mystring;

Assign a value (string literal) to the variable.

$mystring = "Hello world!";

Does the string contains the word "World"?

if($mystring =~ m/World/) { print "Yes"; }

No, it doesn't. The binding operator =~ with the match operator m// does a pattern search on $mystring and returns true if the pattern is found. The pattern is whatever is between the m/ and the trailing /. (Note, there is no such thing as a ~= operator, and using it will give a compile error.)

Does the string contains the word "World", ignoring case?

if($mystring =~ m/World/i) { print "Yes"; }

Yes, it does. The pattern modifier i immediately after the trailing / changes the match to be case-insensitive.

I want "Hello world!" to be changed to "Hello mom!" instead.

$mystring =~ s/world/mom/;
print $mystring;

Prints "Hello mom!". The substitution operator s/// replaces the pattern between the s/ and the middle /, with the pattern between the middle / and last /. In this case, "world" is replaced with the word "mom".

Now change "Hello mom!" to say "Goodby mom!".

$mystring =~ s/hello/Goodbye/;
print $mystring;

This does not substitute, and prints "Hello mom!" as before. By default, the search is case sensitive. As before, use the pattern modifier i immediately after the trailing / to make the search case-insensitive.

Okay, ignoring case, change "Hello mom!" to say "Goodby mom!".

$mystring =~ s/hello/Goodbye/i;
print $mystring;

Prints "Goodby mom!".

Section 2: Extracting substrings

I want to see if my string contains a digit.

$mystring = "[2004/04/13] The date of this article.";
if($mystring =~ m/\d/) { print "Yes"; }

Prints "Yes". The pattern \d matches any single digit. In this case, the search will finish as soon as it reads the "2". Searching always goes left to right.

Huh? Why doesn't "\d" match the exact characters '\' and 'd'?

This is because Perl uses characters from the alphabet to also match things with special meaning, like digits. To differentiate between matching a regular character and something else, the character is immediately preceded by a backslash. Therefore, whenever you read '\' followed by any character, you treat the two together as one symbol. For example, '\d' means digit, '\w' means alphanumeric characters including '_', '\/' means forward slash, and '\\' means match a single backslash. Preceding a character with a '\' is called escaping, and the '\' together with its character is called an escape sequence.

Okay, how do I return the first matching digit from my string?

$mystring = "[2004/04/13] The date of this article.";
if($mystring =~ m/(\d)/) {
	print "The first digit is $1.";

Prints "The first digit is 2." In order to designate a pattern for extraction, one places parenthesis around the pattern. If the pattern is matched, it is returned in the Perl special variable called $1. If there are multiple parenthesized expressions, then they will be in variables $1, $2, $3, etc.

Huh? Why doesn't '(' and ')' match the parenthesis symbols exactly?

This is because the designers of regular expressions felt that some constructs are so common that they should use unescaped characters to represent them. Besides parentheses, there are a number of other characters that have special meanings when unescaped, and these are called metacharacters. To match parenthesis characters or other metacharacters, you have to escape them like '\(' and '\)'. They designed it for their convenience, not to make it easy to learn.

Okay, how do I extract a complete number, like the year?

$mystring = "[2004/04/13] The date of this article.";
if($mystring =~ m/(\d+)/) {
	print "The first number is $1.";

Prints "The first number is 2004." First, when one says "complete number", one really means a grouping of one or more digits. The pattern quantifier + matches one or more of the pattern that immediately precedes it, in this case, the \d. The search will finish as soon as it reads the "2004".

How do I print all the numbers from the string?

$mystring = "[2004/04/13] The date of this article.";
while($mystring =~ m/(\d+)/g) {
	print "Found number $1.";

Prints "Found number 2004. Found number 04. Found number 13. ". This introduces another pattern modifier g, which tells Perl to do a global search on the string. In other words, search the whole string from left to write.

How do I get all the numbers from the string into an array instead?

$mystring = "[2004/04/13] The date of this article.";
@myarray = ($mystring =~ m/(\d+)/g);
print join(",", @myarray);

Prints "2004,04,13". This does the same thing as before, except assigns the returned values from the pattern search into myarray.

Section 3: Common tasks

How do I extract everything between a the words "start" and "end"?

$mystring = "The start text always precedes the end of the end text.";
if($mystring =~ m/start(.*)end/) {
	print $1;

Prints text always precedes the end of the . The pattern .* is two different metacharacters that tell Perl to match everything between the start and end. Specifically, the metacharacter . means match any symbol except new line. The pattern quantifier * means match zero or more of the preceding symbol.

That isn't exactly what I expected. How do I extract everything between "start" and the first "end" encountered?

$mystring = "The start text always precedes the end of the end text.";
if($mystring =~ m/start(.*?)end/) {
	print $1;

Prints text always precedes the . By default, the quantifiers are greedy. This means that when you say .*, Perl matches every character (except new line) all the way to the end of the string, and then works backward until it finds end. To make the pattern quantifier miserly, you use the pattern quantifier limiter ?. This tells Perl to match as few as possible of the preceding symbol before continuing to the next part of the pattern.

'perl' 카테고리의 다른 글

웹 막음, 오픈 (서비스 오픈 전에 사용)  (0) 2009.07.31
CPAN 이용하기  (0) 2008.09.27
펄에서 특정 패턴 확인하기  (0) 2008.07.17
반올림(또는 버림) 함수  (0) 2008.06.07
펄에서 List안에 List를 넣기 (또는 2차원 배열 array)  (0) 2008.06.07
Array 쪽 팁  (0) 2008.06.07
Posted by '김용환'

댓글을 달아 주세요