10-1 �������

¡u³q¥Îªí¥Üªk¡v©Î¡u³q¥Î¦¡¡v¡]Regular expressions¡^¬O¦b UNIX ¥@¬É¤¤µo®i¥X¨Óªº¦r¦ê¤ñ¹ï§Þ¥©¡A¨ä°ò¥»·§©À¬O¥Î¤@®M®æ¦¡Â²³æ¡B¦ý¥\¯à±j¤jªº²Å¸¹¨Ó¤ñ¹ï½ÆÂøªº¦r¦ê¡A¨Ã¥i¹ï²Å¦X¤ñ¹ï±ø¥óªº¦r¦ê¶i¦æ­×§ï©Î¨ä¥L¹Bºâ¡C¨Æ¹ê¤W¡AUNIX ªº³\¦h³nÅé©Î«ü¥O³£¤ä´©³q¥Îªí¥Üªk¡A¨Ò¦p grep¡Bsed¡Bawk¡Bed¡Bvi¡Bemacs µ¥¡C¡]¦ý¬O³o¨ÇªF¦è¤j·§¥u¦³¹³§Ú³o¼Ëªº LKK ¤~·|¥Î§a¡C¡^

Hint
­Y«ö·Ó¦r­±¨Ó½Ķ¡ARegular expressions À³¸Ó½¦¨¡u¥¿³Wªí¥Üªk¡v©Î¡u¥¿³W¦¡¡v¡A¦ý¬O§Ú­Ì¨Ï¥Î¡u³q¥Îªí¥Üªk¡v©Î¡u³q¥Î¦¡¡v¦ü¥G§ó¯à¾A¤Á¦aªí¹F¨ä¥\¯à¡C

¥Ø«e¦U­Ó¥D¬yÂsÄý¾¹³£¤ä´© JavaScript ªº³q¥Îªí¥Üªk¡A¯S§O¾A¥Î©óªí³æ¸ê®ÆªºÅçÃÒ»P­×§ï¡C¨Æ¹ê¤W¡AJavaScript ªº³q¥Îªí¥Üªk©M Perl ¥H¤Î¨ä¥L UNIX ¬ÛÃö«ü¥O´X¥G¤@¼Ò¤@¼Ë¡A¦]¦¹¡A¦b¥»³¹¾Ç¨ìªº³q¥Îªí¥Üªk¡A¤]¥i¥H§¹¥þ¾A¥Î©ó Perl ©Î UNIX ¬ÛÃö«ü¥O¡C¡]¤@³½¨â¦Y¡A¯u¬O¤Ó´Î¤F¡I¡^

Hint
VBScript ªº³q¥Î¦¡¦b¥\¯à¤W©M JavaScript §¹¥þ¬Û¦P¡A¥u¤£¹L©R¥O®æ¦¡¦³©Ò¤£¦P¡A¦³¿³½ìªºÅªªÌ¡A¥i¥H°Ñ¦Òºô¸ô¬ÛÃö¸ê®Æ¡C

JavaScript ªº³q¥Î¦¡¬O¤@­Ó¤º«Øªºª«¥ó¡A¨ä«Øºc¨ç¼Æ¡]Construction functoin¡^¬° RegExp¡A¨å«¬¥Îªk¦p¤U¡G

re = new RegExp("pattern", "flag")
¤W­z¥Îªk¤]¥i¥H²¼g¦¨¤U¦C®æ¦¡¡G
re = /pattern/flag
¨ä¤¤¡Apattern ¬O³q¥Îªí¥Üªkªº¦r¦ê¡Aflag «h¬O¤ñ¹ïªº¤è¦¡¡Cflag ªº­È¥i¯à¦³¤TºØ¡A¤À§O¸ÑÄÀ¦p¤U¡G

Á|¨Ò¨Ó»¡¡A§Ú­Ìªº¨­¥÷ÃÒ¦r¸¹ªº°ò¥»®æ¦¡¡A¬O¥Ñ¤@­Ó­^¤å¦r¥À¥[¤W¤E­Ó¼Æ¦r²Õ¦X¦Ó¦¨¡A¦pªG§Ú­Ì­n¨D¨Ï¥ÎªÌ¿é¤J¨­¥÷ÃÒ¦r¸¹¡A´N¥i¥H¨Ï¥Î JavaScript ªº³q¥Îªí¥Üªk¨ÓÅçÃÒ¨ä®æ¦¡ªº¥¿½T©Ê¡C¨Ò¦p¡A§Ú­Ì¥i¥Î¤U¦Cªí³æ¨Ó­n¨D¨Ï¥ÎªÌ¿é¤J¨­¥÷ÃÒ¦r¸¹¡G

Example¡]regExpID01.htm¡^¡G

¦b¤W¨Ò¤¤¡A§Ú­Ì¥u­n«ö¤U¡uÅçÃÒ¡vªº«ö¶s¡A´N·|©I¥s checkID() ¨ç¼Æ¨Ó¹ï¤å¦rÄæ¦ì¤¤ªº¨­¥÷ÃÒ¦r¸¹¶i¦æÅçÃÒ¡C¬ÛÃö­ì©l½X¦p¤U¡G

­ì©lÀÉ¡]regExpID01.htm¡^¡G¡]¦Ç¦â°Ï°ì«ö¨â¤U§Y¥i«þ¨©¡^
<html>
<head>
<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=big5">
</head>

<body>
<h2 align=center>³q¥Î¦¡¡G²©ö¨­¥÷ÃÒ¦r¸¹ÅçÃÒ</h2>
<hr>

<script>
function checkID(string) {
	re = /^[A-Z]\d{9}$/;
	if (re.test(string))
		alert("¦¨¥\¡I²Å¦X¡u" + re + "¡vªº®æ¦¡¡I");
	else
		alert("¥¢±Ñ¡I¤£²Å¦X¡u" + re + "¡vªº®æ¦¡¡I");
}
</script>
¨­¥÷ÃÒ¦r¸¹¡]²Ä¤@­Ó­^¤å¦r¥À»Ý¤j¼g¡^¡G<input id=idNumber value=A12345678>
<input type=button value="ÅçÃÒ" onClick="checkID(idNumber.value)">

<hr>
</body>
</html>

¦b¤W­z½d¨Ò¤¤¡A/^[A-Z]\d{9}$/ ´N¬O¤@­Ó³q¥Î¦¡¡A»¡©ú¦p¤U¡G

¥Ñ¤W­z»¡©ú¡A¥iª¾ /^[A-Z]\d{9}$/ ´N¥Nªí¥i¥H¤ñ¹ï¨­¥÷ÃÒ¦r¸¹ªº³q¥Î¦¡¡C¦¹¥~¡AidNumber.value ¥Nªí¨Ï¥ÎªÌ¿é¤Jªº¦r¦ê¡Are.test(string) «h¬O³q¥Î¦¡ re ªº¤@­Ó¤èªk¡A·|¶Ç¦^ true ©Î false¡A¥Nªí¤ñ¹ï¬O§_¦¨¥\¡C­Y­n¤£­­©w¬O¤j¼g­^¤å¦r¥À¡A¥u»Ý±N¤W­z½d¨Òªº³q¥Î¦¡§ï¦¨ /^[a-zA-Z]\d{9}$/ ´N¥i¥H¤F¡I

Hint
ª`·N¡G¨Ï¥Î ^ ©M $¡A¥Nªí¡uÀY§À³£­n²Å¦X¡v¡C­Y¤£¥[¤J ^ ©M $¡A¨º»ò /[A-Z]d{9}/ ´N·|¤ñ¹ï¨ì¥ô¦ó§t¦³¨­¥÷ÃÒ¦r¸¹ªº¦r¦ê¡A¨Ò¦p AGF123456789 ©Î¬O F1234567890 µ¥¡C¦]¦¹¡A¥[¤J ^ ©M $ ¥i«OÃÒ¤ñ¹ï¥¿½Tªº¦r¦ê¤@©w¬O¥Ñ¤@­Ó¤j¼g­^¤å¦r¥À¥[¤W¤E­Ó¼Æ¦r©Òºc¦¨¡C

¨Æ¹ê¤W¡A¨­¥÷ÃÒ¦r¸¹¥»¨­´N¦³¤º¦bªº½s½X³W«h¡A³o¨Ç³W«h©M¨Ï¥ÎªÌªº©Ê§O¦³Ãö¡A¦]¦¹­Y­n¹ê²{§¹¾ãªºªí³æÅçÃÒ¡A´N¥²¶·À³¥Î§¹¾ãªº¨­¥÷ÃÒ½s½X³W«h¡AŪªÌ¥i°Ñ¦Ò¥»³¹ªº³Ì«á¤@¸`¡C

¥t¤@­Ó²³æªº¨Ò¤l¡A¬O­n¨D¨Ï¥ÎªÌ¿é¤J«H¥Î¥d¸¹½X¡A³o¬O¤@²Õ 16 ­Ó¼Æ¦rªº¸¹½X¡A¨Ò¦p¡G

Example¡]regExpCreditCardNumber01.htm¡^¡G

·í§Ú­Ì«ö¤U¡uÅçÃÒ¡v«ö¶s®É¡AJavaScript ·|©I¥s¨ç¼Æ checkCreditCard( ) ¨Ó¹ï¶ñ¤Jªº¸ê®Æ¶i¦æÅçÃÒ¡C¬ÛÃö­ì©l½X¦p¤U¡G

­ì©lÀÉ¡]regExpCreditCardNumber01.htm¡^¡G¡]¦Ç¦â°Ï°ì«ö¨â¤U§Y¥i«þ¨©¡^
<html>
<head>
<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=big5">
</head>

<body>
<h2 align=center>³q¥Î¦¡¡G²©ö«H¥Î¥d¥d¸¹ÅçÃÒ</h2>
<hr>

<script>
function checkCreditCard(string) {
	re = /^\d{4}-\d{4}-\d{4}-\d{4}$/;
//	re = /^(\d{4}-){3}\d{4}$/;		// ³oºØ¼gªk¤]¥i¥H¡I
	if (re.test(string))
		alert("¦¨¥\¡I²Å¦X¡u" + re + "¡vªº®æ¦¡¡I");
	else
		alert("¥¢±Ñ¡I¤£²Å¦X¡u" + re + "¡vªº®æ¦¡¡I");
}
</script>
«H¥Î¥d¸¹½X¡G<input id=creditCardNumber value=1234-5678-9012-3456>
<input type=button value="ÅçÃÒ" onClick="checkCreditCard(creditCardNumber.value)">

<hr>
</body>
</html>

¦b¤W¨Ò¤¤¡A«ÜÅãµM¦a¡A/^\d{4}-\d{4}-\d{4}-\d{4}$/ ´N¥Nªí¥¿½Tªº«H¥Î¥d®æ¦¡¡C«Ü©úÅ㪺¡A¨Ï¥Î³q¥Î¦¡·|Åýµ{¦¡½X²¼ä«Ü¦h¡A¦Ó¥B·|¤j¤j´£°ªµ{¦¡½Xªº¥¿½T©Ê¡C¡]½Ð©M«e­±³¹¸`ªºÃþ¦ü½d¨Ò formValidation02.htm ¤ñ¸û¬Ý¬Ý¡C¡^¦ý­nª`·Nªº¬O¡A«H¥Î¥d¥d¸¹¥»¨­´N¦³¤º¦bªº¸û½ÆÂø½s½X³W«h¡A¦]¦¹­Y­n¹ê²{§¹¾ãªºªí³æÅçÃÒ¡A´N¥²¶·À³¥Î§¹¾ãªº«H¥Î¥d¥d¸¹½s½X³W«h¡AŪªÌ¥i°Ñ¦Ò¥»³¹ªº³Ì«á¤@¸`¡C

¦pªG­«½Æªº³¡¤À¦h©ó¤@­Ó¦r¥À¡A§Ú­Ì´N¥²¶·±N»Ý­n­«½Æªº³¡¤À©ñ¦b¤p¬A¸¹¤º¡A¦A¥[¤W¥Ñ¤j¬A¸¹¥]§¨ªº­«½Æ¦¸¼Æ¡A¨Ò¦p¡A¤W­z½d¨Òªº³q¥Î¦¡ /^\d{4}-\d{4}-\d{4}-\d{4}$/¡A¤]¥i¥H¼g¦¨ /^(\d{4}-){3}\d{4}$/¡A½Ð¸Õ¸Õ¬Ý¡I

¤U¤@­Ó¨Ò¤l¡A«h¬O¥Î³q¥Îªí¥Üªk¨ÓÅçÃҨϥΪ̪º­^¤å¦W¦r¡A¨Ò¦p¡G

Example¡]regExpEnglishName01.htm¡^¡G

·í§Ú­Ì«ö¤U¡uÅçÃÒ¡v«ö¶s®É¡AJavaScript ·|©I¥s¨ç¼Æ checkEnglishName( ) ¨Ó¹ï¶ñ¤Jªº¸ê®Æ¶i¦æÅçÃÒ¡C¬ÛÃö­ì©l½X¦p¤U¡G

­ì©lÀÉ¡]regExpEnglishName01.htm¡^¡G¡]¦Ç¦â°Ï°ì«ö¨â¤U§Y¥i«þ¨©¡^
<html>
<head>
<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=big5">
</head>

<body>
<h2 align=center>³q¥Î¦¡¡G²©ö­^¤å¦W¦rÅçÃÒ</h2>
<hr>

<script>
function checkEnglishName(string) {
	re1 = /^[A-Za-z\-]+\s+[A-Za-z\-]+$/;
	re2 = /^[A-Za-z\-]+\s+[A-Za-z\-]+\s+[A-Za-z\-]+$/;
	if (re1.test(string) || re2.test(string))
		alert("¦¨¥\¡I²Å¦X¡u" + re1 + "¡v©Î¡u" + re2 + "¡vªº®æ¦¡¡I");
	else
		alert("¥¢±Ñ¡I¤£²Å¦X¡u" + re1 + "¡v©Î¡u" + re2 + "¡vªº®æ¦¡¡I");
}
</script>
§Aªº­^¤å¥þ¦W¡]®æ¦¡¡GFirst Last ©Î First Middle Last¡^¡G<input id=englishName value="Jyh-Shing Roger Jang">
<input type=button value="ÅçÃÒ" onClick="checkEnglishName(englishName.value)">

<hr>
</body>
</html>

¹ï©ó¤W­z½d¨Òµ{¦¡¡A§Ú­Ì»¡©ú¦p¤U¡G

¦]¦¹ re1 = /^[A-Za-z\-]+\s+[A-Za-z\-]+$/ ¥i¥H¤ñ¹ï¥Ñ¨â­Ó¦r·J©Ò§Î¦¨ªº­^¤å¦W¦r¡A¨Ò¦p Michael Jordan¡F¦Ó re2 = /^[A-Za-z\-]+\s+[A-Za-z\-]+\s+[A-Za-z\-]+$/ «h¥i¥H¤ñ¹ï¥Ñ¤T­Ó¦r·J©Ò§Î¦¨ªº­^¤å¦W¦r¡A¨Ò¦p¡AJyh-Shing Roger Jang¡C

¤U¤@­Ó¨Ò¤l¡A«h¬O¥Î³q¥Îªí¥Üªk¨ÓÅçÃÒ¹q¤l¶l¥ó¡A¨Ò¦p¡G

Example¡]regExpEmail01.htm¡^¡G

·í§Ú­Ì«ö¤U¡uÅçÃÒ¡v«ö¶s®É¡AJavaScript ·|©I¥s¨ç¼Æ checkEmail( ) ¨Ó¹ï¶ñ¤Jªº¸ê®Æ¶i¦æÅçÃÒ¡C¬ÛÃö­ì©l½X¦p¤U¡G

­ì©lÀÉ¡]regExpEmail01.htm¡^¡G¡]¦Ç¦â°Ï°ì«ö¨â¤U§Y¥i«þ¨©¡^
<html>
<head>
<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=big5">
</head>

<body>
<h2 align=center>³q¥Î¦¡¡G²©ö¹q¤l¶l¥ó®æ¦¡ÅçÃÒ</h2>
<hr>

<script>
function checkEmail(string) {
	re = /^.+@.+\..{2,3}$/;
	if (re.test(string))
		alert("¦¨¥\¡I²Å¦X¡u" + re + "¡vªº®æ¦¡¡I");
	else
		alert("¥¢±Ñ¡I¤£²Å¦X¡u" + re + "¡vªº®æ¦¡¡I");
}
</script>
¹q¤l¶l¥ó¡G<input id=email value="jang@cs.nthu.edu.t">
<input type=button value="ÅçÃÒ" onClick="checkEmail(email.value)">

<hr>
</body>
</html>

¹ï©ó¦¹½d¨Ò©Ò¥Î¨ìªº³q¥Î¦¡ /^.+@.+\..{2,3}$/¡A»¡©ú¦p¤U¡G

¦]¦¹ /^.+@.+\..{2,3}$/ ¥i¥Î¨Ó¤ñ¹ï¤@¯ëªº email ±b¸¹¡A¨Ò¦p test@cs.nthu.edu.tw ©Î¬O roger_jang@mathworks.com µ¥¡A¦ý¬O¦¹³q¥Î¦¡¨Ã«Dºw¤ô¤£º|¡A¦³¨Ç¤£¦X®æªº email ±b¸¹¤]·|¤ñ¹ï¦¨¥\¡A¨Ò¦p " @math.com"¡A©Î¬O "test@ .tw"¡A©Î¬O "aa@bb.zz"¡C­Y­nÁ׶}§t¦³ªÅ¥Õªº email ±b¸¹¡A½Ð¨£¤U¦C½d¨Ò¡G

Example¡]regExpEmail02.htm¡^¡G

¬ÛÃö­ì©l½X¦p¤U¡G

­ì©lÀÉ¡]regExpEmail02.htm¡^¡G¡]¦Ç¦â°Ï°ì«ö¨â¤U§Y¥i«þ¨©¡^
<html>
<head>
<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=big5">
</head>

<body>
<h2 align=center>³q¥Î¦¡¡G¹q¤l¶l¥ó®æ¦¡ÅçÃÒ¡]¥i¥HÁ׶}§t¦³ªÅ¥Õªº¹q¤l¶l¥ó±b¸¹¡^</h2>
<hr>

<script>
function checkEmail(string) {
	re = /^[^\s]+@[^\s]+\.[^\s]{2,3}$/;
	if (re.test(string))
		alert("¦¨¥\¡I²Å¦X¡u" + re + "¡vªº®æ¦¡¡I");
	else
		alert("¥¢±Ñ¡I¤£²Å¦X¡u" + re + "¡vªº®æ¦¡¡I");
}
</script>
¹q¤l¶l¥ó¡G<input id=email value="jang@cs.n thu.edu.tw">
<input type=button value="ÅçÃÒ" onClick="checkEmail(email.value)">

<hr>
</body>
</html>

¹ï©ó¦¹½d¨Ò©Ò¥Î¨ìªº³q¥Î¦¡ /^[^\s]+@[^\s]+\.[^\s]{2,3}$/¡A»¡©ú¦p¤U¡G

Hint
½Ðª`·N¡G^ ¦b¤@¯ë³q¥Îªí¥Üªkªº·N¸q¬O¡u¦r¦ê¶}©lªº¦ì¸m¡v¡A¦ý¬O¤@¥¹©ñ¦b¤¤¬A©·¤º¡A«h¬O¥Nªí¡u§_©w¡v©Î¡u«D¡v¡C

¦b¥H¤Uªº½d¨Ò¤¤¡A§Ú­Ì³]­p¤F¤@­Óªí³æ¡A¥i¥HÅý¨Ï¥ÎªÌ¿é¤J¥ô·N¦r¦ê¡B³q¥Î¦¡¡A¥H¤Î¤ñ¹ï¿ï¶µ¡A¨Ã¦b³q¥Î¦¡¤ñ¹ï«á¡A¦C¥X¤ñ¹ï¨ìªº¦r¦ê¡AŪªÌ­Ì¥i¥H§Q¥Î¦¹½d¨Ò¡A¤ÏÂкt½m¡A¥H¼W¶i¹ï©ó³q¥Î¦¡ªºÁA¸Ñ¡G

Example¡]regExpTest01.htm¡^¡G

¤W­z½d¨Òªº­ì©lÀɦp¤U¡G

­ì©lÀÉ¡]regExpTest01.htm¡^¡G¡]¦Ç¦â°Ï°ì«ö¨â¤U§Y¥i«þ¨©¡^
<html>
<head>
<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=big5">
</head>

<body>
<h2 align=center>³q¥Î¦¡¡G§¹¾ã´ú¸Õ­¶</h2>
<hr>

<script>
function showMatched(form){
	var regexp = new RegExp(form.pattern.value, form.flag.value);
	var str = form.string.value;
	var matched = str.match(regexp);
	if (matched) {
		var dispstr = matched.length + " ­Ó¤ñ¹ï¨ìªº¦r¦ê¡G";
		for (var i=0; i<matched.length; i++)
			dispstr = dispstr + "\n" + matched[i];  
		alert(dispstr);
	} else
		alert("¨S¦³¤ñ¹ï¨ì¥ô¦ó¦r¦ê¡I");
}
</script>
<form>
<table align=center>
<tr><td align=right>¦r¦ê¡G
<td><input type=text size=30 name=string value="There are 10 rookies coming at 3 o'clock!">
<tr><td align=right>³q¥Î¦¡¡G
<td><input type=text size=30 name=pattern value=" \w+ "> (½d¨Ò¡G\d{2,3}, T.*a, T.*?a, \b\w+\b)
<tr><td align=right>¿ï¶µ¡G
<td><input type=text size=30 name=flag value="g"> (g, i, or gi)
<tr><td align=right><br>
<td><input type="button" value="Åã¥Ü¤ñ¹ï¨ìªº¦r¦ê" onClick="showMatched(this.form)"><input type="reset">
</table>
</form>

<hr>
</body>
</html>

¦b¤W­z½d¨Ò¤¤¡A§Ú­Ì¨Ï¥Î¤F¦r¦êªº match() ¤èªk¡A¨Ó¹ï³q¥Î¦¡¶i¦æ¤ñ¹ï¡A¦]¦¹ matched = str.match(regexp) ¥i±N¤ñ¹ï¨ìªº¦r¦ê°e¨ì¤@­Ó°}¦C¡A¥H«K«áÄò³B²z¡C

Hint
¦b¤W­z½d¨Ò¤¤¡A§Ú­Ìªº³q¥Î¦¡¬O¡u w+ ¡v¡]ª`·N«e«á³£¦³ªÅ®æ¡^¡A¦]¦¹©Ò§ä¨ìªº¦r¦ê¬O¡u are ¡v¡B¡u rookies ¡v¡B¡u at ¡v¡]«e«á¤]³£¦³ªÅ®æ¡^¡A¦p¦ó¤~¯à§ä¨ì©Ò¦³«e«á¦³ªÅ®æªº³æ¦r©O¡H½Ð¦U¦ìŪªÌ¦Û¦æ¬ã¨s¬Ý¬Ý¡I

¦b¶i¦æªí³æ¸ê®ÆÅçÃÒ¤§«e¡A§Ú­ÌÀ³¥ý¶i¦æªí³æ¸ê®Æ­×§ï¡A¨Ò¦p®³±¼¤£¥²­nªºªÅ®æ¡B­^¤å¦r¥À¤j¤p¼gÂà´«µ¥¡A³o¨Ç¤u§@¤]¥i¥H¥Ñ¦r¦êªº replace() ¤èªk©Î³q¥Î¦¡ªº exec() ¤èªk¨Ó¹F¦¨¡A³o¬O§Ú­Ì¤U¤@¸`ªº¥DÃD¡C


JavaScript µ{¦¡³]­p»PÀ³¥Î¡G¥Î©óºô­¶¥Î¤áºÝ