4-8 �r�ꪺ�N��

«e­±´X¸`¤¶²Ð³q¥Î¦¡¡A¥H¤Î¦p¦ó§Q¥Î³q¥Î¦¡¨Ó¤ñ¹ï¤Î©â¨ú¯S©w¦r¦ê¡A¥»¸`±N¤¶²Ð¦p¦ó§Q¥Î³q¥Î¦¡¨Ó¶i¦æ¦r¦êªº¥N´«¡A¥D­nªº«ü¥O¬O regexprep¡C¨Ò¦p¡A­Y­n±N©Ò¦³¡ub ©M t ¤¤¶¡¦Ü¤Ö§¨¤@­Ó¥À­µ¡vªº¦r¦ê¥N´«¬° xxx¡A¥i¨£¤U¦C½d¨Ò¡G

Example 1: 04-³q¥Î¹Bºâ¦¡/regExpRep01.mstr = 'I bet there is a bat in the boat!'; pat = 'b[aeiou]+t'; newStr = regexprep(str, pat, 'xxx'); fprintf('%s\n', newStr);I xxx there is a xxx in the xxx!

¥t¤@­Ó½d¨Ò¡A«h¬O±N¤@¦C¦r¦ê¤¤¡A³sÄò¥X²{ªº¦h­ÓªÅ¥Õ¦r¤¸¡AÀ£ÁY¦¨¤@­ÓªÅ¥Õ¦r¤¸¡A¦p¤U¡G

Example 2: 04-³q¥Î¹Bºâ¦¡/regExpRep02.mstring = 'Draft beer, not people.'; pattern = '\s+'; string2 = regexprep(string, pattern, ' '); % ±N¦h­ÓªÅ¥ÕÀ£ÁY¦¨¤@­Ó fprintf('­ì¦r¦ê¡G%s\n', string); fprintf('­×§ï«á¡G%s\n', string2);­ì¦r¦ê¡GDraft beer, not people. ­×§ï«á¡GDraft beer, not people.

¦b¨Ï¥Î regexprep ¶i¦æ¦r¦ê¥N´«ªº¹Lµ{¤¤¡A MATLAB ¥i¥H±N¤p¬A©·¤ñ¹ï¨ìªº¤l¦r¦ê¼È®ÉÀx¦s¨ìÅÜ¼Æ $1, $2, $3 µ¥¡A¥H«K©ó³B²z«á¦A´¡¦^­ì¦r¦ê¡A¨Ò¦p¡G

Example 3: 04-³q¥Î¹Bºâ¦¡/regExpRep03.mstr = 'I walk up, he walks up, we are all walking up.'; pat = 'walk(\w*) up'; newStr = regexprep(str, pat, 'sleep$1 tight'); fprintf('%s\n', newStr);I sleep tight, he sleeps tight, we are all sleeping tight.

¨Ï¥ÎÃþ¦üªº¤èªk¡A§Ú­Ì¤]¥i¥H±N¤@­Ó¦r¦êªº«e¨â­Ó­^¤å¦r¹ï½Õ¡A¦p¤U¡G

Example 4: 04-³q¥Î¹Bºâ¦¡/regExpRep04.mstr = 'are you ready'; pat = '^([^ ]+) +([^ ]+)'; rep = '$2 $1'; str2 = regexprep(str, pat, rep); fprintf('­ì¦r¦ê¡G%s\n', str); fprintf('­×§ï«á¡G%s\n', str2);­ì¦r¦ê¡Gare you ready ­×§ï«á¡Gyou are ready

¦b¨Ï¥Î regexprep «ü¥O®É¡A¥i¥H¦b²Ä¥|­Ó¿é¤JÅܼƿé¤J¨ä¥L¦r¦ê¡A¥H¥Nªí¤£¦Pªº¥N´«¤è¦¡¡C¨Ò¦p¡A¡¥ignorecase¡¦ ¥Nªí¥i¥H¶i¦æ¡u¤j¤p¼g¤£¤À¡vªº¤ñ¹ï»P¥N´«¡A¡¥once¡¦ ¥Nªí¥u¥N´«²Ä¤@­Ó¤ñ¹ï²Å¦Xªº¦r¦ê¡A¨ä¥L¿ï¶µ½Ð¬d¬Ý regexprep ªº½u¤W¤ä´©¡]¦b MATLAB ¿é¤J¡udoc regexprep¡v§Y¥iÅã¥Ü½u¤W¤ä´©¡^¡C


MATLABµ{¦¡³]­p¡G¶i¶¥½g