|
![](icon/private_name/1.gif) |
1³â Àü |
Á¤±ÔÇ¥Çö½Ä
À§Ä¡¿Í À̽ºÄÉÀÌÇÎ
^who -> ¸Ç ¾Õ who ã±â
who$ -> ¸Ç µÚ who ã±â
$ -> $ ã±â
^$ -> ¸Ç ¾Õ $ ã±â
$$ -> ¸Ç µÚ $ ã±â
-> ã±â
¸ðµç ¹®ÀÚ ±×·ì
. -> ¾î¶°ÇÑ ¹®ÀÚ ¸ðµÎ¸¦ °¡¸®Å´
...... -> ¹®ÀÚ 6µ¢¾î¸®¸¦ °¡¸®Å´
. -> . ã±â
... -> .(any character). ã±â
ƯÁ¤ ¹®ÀÚ ¹üÀ§
[oyu] -> o or y or u ã±â
[dH]. -> d(any character) or H(any character) ã±â -> dy, HH, do ... µîµî
[owy][yow] -> ow, oy, ww, ... (¹®ÀÚ 2°³ ã±â)
[C-K] -> CºÎÅÍ K ¹üÀ§¿¡ ÀÖ´Â ¾ËÆĺª ¹®ÀÚ 1°³ ã±â -> [CDEFGHIJK]¿Í °°Àº ÀǹÌ.
[a-d] -> aºÎÅÍ d±îÁö
[2-6] -> 2ºÎÅÍ 6±îÁö
[C-Ka-d2-6] -> C~K or a~d or 2~6 »çÀÌÀÎ ¹®ÀÚ 1°³ ã±â
[^CDghi45] -> C, D, g, h, i, 4, 5°¡ ¾Æ´Ñ ¹®ÀÚ Ã£±â -> ^°¡ []¾È¿¡ ¾²ÀÌ¸é ºÎÁ¤ÀÇ ÀǹÌ
[^W-Z] -> W, X, Y, Z°¡ ¾Æ´Ñ ¹®ÀÚ Ã£±â
¼ºêÆÐÅÏ
(on|ues|rida) -> ¹®ÀÚ¿¿¡¼ onÀ̰ųª uesÀ̰ųª ridaÀÎ ºÎºÐ ã±â
(Mon|Tues|Fri)day -> MondayÀ̰ųª TuesdayÀ̰ųª FridayÀÎ ºÎºÐ ã±â
..(id|esd|nd)ay -> ¹®ÀÚ 2°³ ¾Æ¹«°Å³ª¿Í ay»çÀÌ°¡ idÀ̰ųª esdÀ̰ųª ndÀÎ ºÎºÐ ã±â
¼ö·®ÀÚ
a*b -> ¹®ÀÚ¿¿¡¼ a°¡ 0 ~ ¿©·¯°³ÀÌ°í µÚ¿¡ b°¡ ºÙ´Â ºÎºÐ ã±â -> b, ab, aab, aaab ã¾ÆÁü.
a+b -> b ¾Õ¿¡ a°¡ 1°³ ÀÌ»óÀÎ ºÎºÐ ã±â
a?b -> b ¾Õ¿¡ a°¡ ¾ø°Å³ª 1°³ÀÎ ºÎºÐ ã±â
[-@]* -> [] È帱º¿¡ ¼ÓÇÏ´Â ¹®ÀÚ°¡ ¾ø°Å³ª ¿©·¯°³ÀÎ ºÎºÐ ã±â -> -@-, --
*+ -> *ÀÌ 1°³ ÀÌ»óÀÎ ºÎºÐ
-@+- -> ¾Õ°ú µÚ°¡ -ÀÌ°í, @°¡ 1°³ ÀÌ»óÀÎ ºÎºÐ ã±â -> -@-, -@@-, -@@@-, ...
[^ ]+ -> °ø¹éÀÌ ¾Æ´Ñ °ÍÀÌ 1°³ ÀÌ»óÀÎ ºÎºÐ ã±â
-X?XX?X -> -XX, -XXXX, -XXX
-@?@?@?- -> --, -@-, -@@-, -@@@-
.{5} -> ¾î¶°ÇÑ ¹®ÀÚÀÌ´ø°£¿¡ 5±ÛÀÚ ¼±ÅÃÇϱâ
[els]{1,3} -> e¶Ç´Â l¶Ç´Â s Á¶ÇÕÀ¸·Î 1~3°³ ¹®ÀÚ¸¸ ¼±Åà -> e, ee, el, ess
[a-z]{3,} -> aºÎÅÍ z±îÁö ¹®ÀÚÀÇ Á¶ÇÕ Áß 3°³ ÀÌ»óÀÎ ºÎºÐ ¼±ÅÃ
AB*A == AB{0,}A
AB+A == AB{1,}A
AB?A == AB{0,1}A
r.* -> r, rfjdaklsfjslk, riinlk dkfjls (°ø¹éµµ ´Ù ¼±ÅõÊ.)
r.*? -> r¸¸ °¡´É -> ¼ö·®ÀÚ µÚ¿¡ ?°¡ ¿À¸é ÇØ´ç ¼ö·®ÀÚ°¡ °¡Áú ¼ö ÀÖ´Â ÃÖ¼Ò ¼ö¸¦ ¼±ÅÃÇÏ°Ô µÊ.
r.+? -> rx, rd, rb, rq, rr
r.?? -> r¸¸ °¡´É
°æ°è
w -> word(¾ËÆĺª, ¼ýÀÚ, _)¸¸ ¼±ÅÃ
w == [A-z0-9_]
W -> word°¡ ¾Æ´Ñ °Í ¼±ÅÃ
d -> ¼ýÀÚ¸¸ ¼±ÅÃ
D -> ¼ýÀÚ°¡ ¾Æ´Ñ °Í ¼±ÅÃ
b -> ´Ü¾î boundary
bw -> ´Ü¾îÀÇ Ã¹ ±ÛÀÚ ¼±ÅÃ
wb -> ´Ü¾îÀÇ ¸¶Áö¸· ±ÛÀÚ ¼±ÅÃ
bwb -> ´Ü¾îÀÇ ±æÀÌ°¡ 1ÀÎ ´Ü¾î ¼±ÅÃ
bw+b -> ´Ü¾î º°·Î ¼±ÅõÊ.
B.B -> ´Ü¾î °æ°è(¾Õ, µÚ)¸¦ Á¦¿ÜÇÑ ³ª¸ÓÁö ¼±ÅõÊ.
A -> ´Ü¾î °æ°èÀÇ ½ÃÀÛÁ¡À» °¡¸®Å´ (±ÛÀÚ °¡¸®Å°Áö ¾ÊÀ½.)
A... -> ½ÃÀÛ °æ°è·ÎºÎÅÍ ±ÛÀÚ 3°³
...Z -> ³¡ °æ°è·ÎºÎÅÍ ±ÛÀÚ 3°³
<?php
// php¿¡¼´Â Á¤±ÔÇ¥Çö½ÄÀ» /(Á¤±ÔÇ¥Çö½Ä)/(pattern modifier) À¸·Î ÀÛ¼ºÇÑ´Ù.
// match µÇ´Â ºÎºÐ ÀÖÀ¸¸é 1, ¾øÀ¸¸é 0 ¸®ÅÏ
//
// pattern modifier
// i: ÆÐÅÏ ¸ÅĪÀ» ÇÒ ¶§, ´ë¼Ò¹®ÀÚ¸¦ ±¸ºÐÇÏÁö ¾Êµµ·Ï ÇÑ´Ù.
// m: ¸ÖƼ¶óÀÎÀ¸·Î ÀÛµ¿. (^³ª $¸¦ ¿©·¯ÁÙ¿¡¼ »ç¿ëÇÒ ¶§, matchµÇ´Â ºÎºÐµé Áß ¸Ç ¾Õ/¸Ç µÚ¿¡¸¸ Àû¿ëµÊ.)
if (preg_match("/php/i", "PHP is the web scripting language of choice.")) {
// PHP¿¡¼ °É¸². -> match -> 1 ¸®ÅÏ.
echo "A match was found. (1)<br>";
} else {
echo "A match was not found. (1)<br>";
}
// b´Â ´Ü¾îÀÇ °æ°è¸¦ ÀǹÌÇÑ´Ù. ±×·¸±â ¶§¹®¿¡ b·Î °¨½ÎÁø webÀº webÀ̶ó´Â µ¶¸³µÈ ´Ü¾î¸¦ ÀǹÌÇÑ´Ù. website´Â ÀÌ Á¶°Ç¿¡ ÇØ´çÇÏÁö ¾Ê´Â´Ù.
if (preg_match("/bwebb/i", "PHP is the web scripting language of choice.")) {
echo "A match was found. (2)<br>";
} else {
echo "A match was not found. (2)<br>";
}
if (preg_match("/bwebb/i", "PHP is the website scripting language of choice.")) {
echo "A match was found. (3)<br>";
} else {
echo "A match was not found. (3)<br>";
}
$subject = 'coding everybody http://opentutorials.org egoing@egoing.com 010-0000-0000';
preg_match('/(http://w+.w+)s(w+@w+.w+)/', $subject, $match);
echo '<pre>';
var_dump($match);
echo '</pre>';
echo "homepage: " . $match[1] . '<br>';
echo "email: " . $match[2] . '<br>';
// get host name from url
// ?: À» ºÙ¿©ÁÜÀ¸·Î½á ÇØ´ç ºÎºÐÀÌ match2 ¹è¿¿¡ ´ã±âÁö ¾Êµµ·Ï
preg_match("/^(?:http://)?([^/]+)/i", "http://www.php.net/index.html", $match2);
echo '<pre>';
var_dump($match2);
echo '</pre>';
$host = $match2[1]; // www.php.net
// get last two segments of host name
preg_match("/[^.]+.[^.]+$/i", $host, $match3);
echo "domain name is: {$match3[0]}";
// back reference ÁöÁ¤
// key·Î ²¨³»¿Ã ¼ö ÀÖÀ½.
$str = 'foobar: 2008';
preg_match('/(?P<name>w+): (?P<digit>d+)/i', $str, $match4);
echo '<pre>';
var_dump($match4);
echo '</pre>';
// preg replace 1
$string = 'April 15, 2003';
$pattern = '/(w+) (d+), (d+)/i';
$replacement = '${1}1, $3'; // $1, ${1} ù¹ø° ¼ºêÆÐÅÏ °ªÀε¥, ${1}Àº ´Ù¸¥¹®ÀÚ¿Í ¼¯¾î ¾µ ¶§ »ç¿ë
echo preg_replace($pattern, $replacement, $string);
// preg replace 2
$string2 = 'The quick brown fox jumped over the lazy dog.';
$patterns = array();
$patterns[0] = '/quick/';
$patterns[1] = '/brown/';
$patterns[2] = '/fox/';
$replacements = array();
$replacements[2] = 'bear';
$replacements[1] = 'black';
$replacements[0] = 'slow';
echo preg_replace($patterns, $replacements, $string2);
// preg replace 3
$patterns = array(
'/(19|20)(d{2})-(d{1,2})-(d{1,2})/',
'/^s*{(w+)}s*=/'
);
$replace = array(
'3/4/12',
'$1 ='
);
echo preg_replace($patterns, $replace, '{startDate} = 1999-5-27'); // $startDate = 5/27/1999 |
|
̵̧ : 106 |
̵̧
¸ñ·Ï
|
|