Python Re
1. Basic
²Î¿¼¡¶PythonÕýÔò±í´ïʽ²Ù×÷Ö¸ÄÏ¡·
Ä£¿ére£¬perl·ç¸ñµÄÕýÔò±í´ïʽ
regex²¢²»Äܽâ¾öËùÓеÄÎÊÌ⣬ÓÐʱºò»¹ÊÇÐèÒª´úÂë
regex»ùÓÚÈ·¶¨ÐԺͷÇÈ·¶¨ÐÔÓÐÏÞ×Ô¶¯»ú
2. ×Ö·ûÆ¥Åä(ÑÐò½¥½ø)
Ôª×Ö·û
. ^ $ * + ? { [ ] \ | ( )
1) "[" ºÍ "]"³£ÓÃÀ´Ö¸¶¨Ò»¸ö×Ö·ûÀà±ð£¬Ëùν×Ö·ûÀà±ð¾ÍÊÇÄãÏëÆ¥ÅäµÄÒ»¸ö×Ö·û¼¯¡£Èç[abc]»òÕß[a-c]¡£Àà±ðÖв»Ðè·Ö¸ô·ûÖ®ÀàµÄ¶«Î÷
Ôª×Ö·ûÔÚÀà±ðÀï²¢²»Æð×÷Óá£ÀýÈ磬[akm$]½«Æ¥Åä×Ö·û"a", "k", "m", »ò "$" ÖеÄÈÎÒâÒ»¸ö£»"$"ͨ³£ÓÃ×÷Ôª×Ö·û£¬µ«ÔÚ×Ö·ûÀà±ðÀÆäÌØÐÔ±»³ýÈ¥£¬»Ö¸´³ÉÆÕͨ×Ö·û¡£
2) ^
²¹¼¯¡£°Ñ"^"×÷ΪÀà±ðµÄÊ׸ö×Ö·û£»ÆäËüµØ·½µÄ"^"Ö»»á¼òµ¥Æ¥Åä "^"×Ö·û±¾Éí¡£ÀýÈ磬[^5] ½«Æ¥Åä³ý "5" Ö®ÍâµÄÈÎÒâ×Ö·û¡£
3) \
ºÜÅ££¡
\d Æ¥ÅäÈκÎÊ®½øÖÆÊý£»ËüÏ൱ÓÚÀà [0-9]¡£
\D Æ¥ÅäÈκηÇÊý×Ö×Ö·û£»ËüÏ൱ÓÚÀà [^0-9]¡£
\s Æ¥ÅäÈκοհ××Ö·û£»ËüÏ൱ÓÚÀà [ \t\n\r\f\v]¡£
\S Æ¥ÅäÈκηǿհ××Ö·û£»ËüÏ൱ÓÚÀà [^ \t\n\r\f\v]¡£
\w Æ¥ÅäÈκÎ×ÖĸÊý×Ö×Ö·û£»ËüÏ൱ÓÚÀà [a-zA-Z0-9_]¡£
\W Æ¥ÅäÈκηÇ×ÖĸÊý×Ö×Ö·û£»ËüÏ൱ÓÚÀà [^a-zA-Z0-9_]¡£
4) .
. Æ¥Åä³ýÁË»»ÐÐ×Ö·ûÍâµÄÈκÎ×Ö·û£¬ÔÚ alternate ģʽ£¨re.DOTALL£©ÏÂËüÉõÖÁ¿ÉÒÔÆ¥Åä»»ÐС£"." ͨ³£±»ÓÃÓÚÄãÏëÆ¥Åä“ÈκÎ×Ö·û”µÄµØ·½¡£
3. Öظ´
1) *
Æ¥Åä0~n¸ö×Ö·û£¬Ò»°ã²»³¬¹ý×î´óÊý
ÕâÖÖÖظ´ÊÇ“Ì°À·µÄ”
×÷ÓÃÓÚÇ°Ò»¸öÀà±ð»òÕß×Ö·û¡£Èçca*t×÷ÓÃÓÚa£¬a[bc]*d×÷ÓÃÓÚ[bc]
2) +
Æ¥Åä1~n
3) ?
Æ¥Åä0~1
4) {m, n}
ÆäÖÐ m ºÍ n ÊÇÊ®½øÖÆÕûÊý¡£¸ÃÏÞ¶¨·ûµÄÒâ˼ÊÇÖÁÉÙÓÐ m ¸öÖظ´£¬ÖÁ¶àµ½ n ¸öÖظ´¡£¾Ù¸öÀý×Ó£¬a/{1,3}b ½«Æ¥Åä "a/b"£¬"a//b" ºÍ "a///b"¡£Ëü²»ÄÜÆ¥Åä "ab" ÒòΪûÓÐб¸Ü£¬Ò²²»ÄÜÆ¥Åä "a////b" £¬ÒòΪÓÐËĸö¡£
ÆäËûÈý¸öÏÞ¶¨·û¶¼¿ÉÒÔÓÃÕâÑù·½Ê½À´±íʾ¡£ {0,} µÈͬÓÚ *£¬{1,} µÈͬÓÚ +£¬¶ø{0,1}ÔòÓë ? Ïàͬ¡£Èç¹û¿ÉÒԵĻ°£¬×îºÃʹÓà *£¬+£¬»ò?¡£ºÜ¼òµ¥ÒòΪËüÃǸü¶ÌÒ²ÔÙÈÝÒ׶®¡£×îºó×ÜÄÜÕâÑù£¬ÓиöͬһµÄ
4. Àý×Ó
1) Éú³ÉRE
import re
#RE is a string
p = re.compile('[a-z]+')
print p
2)ʹÓÃ
if p.match(''):
print 'matched'
else:
print 'not matched'
#results: not matched
3) ·µ»ØÖµ
m = p.match('abcdef')
if m:
print 'matched again'
else:
print 'no
Ïà¹ØÎĵµ£º
Python
µÄ
C/C++
À©Õ¹
By phidoit@gmail.com
¿ÉÀ©Õ¹ÐÔÊÇ
Python
µÄÒ»´óÌØÉ«£¬Ò»·½Ã棬ÓÉÓÚ
Python
ÊǽâÊÍÖ´Ðеģ¬Õâµ¼ÖÂÔËÐÐËÙ ......
SQLITEÊÇÒ»¿î·Ç³£Ð¡ÇɵÄǶÈëʽ¿ªÔ´Êý¾Ý¿âÈí¼þ£¬Ò²¾ÍÊÇ˵ûÓжÀÁ¢µÄά»¤½ø³Ì£¬ËùÓеÄά»¤¶¼À´×ÔÓÚ³ÌÐò±¾Éí¡£ËüʹÓÃÒ»¸öÎļþ´æ´¢Õû¸öÊý¾Ý¿â£¬²Ù×÷Ê®·Ö·½±ã¡£ËüµÄ×î´óÓŵãÊÇʹÓ÷½±ã£¬¹¦ÄÜÏà±ÈÓÚÆäËü´óÐÍÊý¾Ý¿âÀ´Ëµ£¬È·ÊµÓÐЩ²î¾à¡£µ«ÊÇÐÔÄܱíÏÖÉÏ£¬SQLITE²¢²»Ñ·É«¡£ÂéȸËäС£¬ÎåÔà¾ãÈ«£¬ sqlite ʵÏÖÁ˶àÊý sql-92 µÄ±ê×¼£¬±È ......
ÔÚPythonÖУ¬¿ÉÒÔʹÓÃFunctionÀ´ÊµÏÖ´úÂëÖØÓá£Èç¹ûÐèÒªÖØÓÃÒ»×éfunction£¬ÔòÐèҪʹÓÃModules¡£
¶ÔÓÚModules£¬¿ÉÒÔʹÓÃÁ½ÖÖ·½·¨±àд¡£µÚÒ»¡¢Ò²ÊÇ×î¼òµ¥µÄ·½·¨£¬¾ÍÊÇʹÓÃPython±àд¡£¿ÉÒÔ´´½¨Ò»¸öºó׺ÃûΪ.pyµÄÎļþ£¬ÔÚÆäÖбàдº¯ÊýºÍ±äÁ¿¡£ÁíÍâÒ»ÖÖ·½·¨ÊÇÖ ......
ÕâÊÇÎÒÒÔǰдµÄÒ»¸ösocketСӦÓã¬ÆäÖÐҲ˳±ãÓõ½ÁËÒ»µãµã¶àÏ̵߳Ķ«Î÷¡£
Õâ¸ösocket¿Í»§¶ËÊÇÎÒΪÁË·½±ã¹«Ë¾µÄ³äֵϵͳµ÷ÊÔ¶øдµÄÒ»¸öСӦÓá£ÓÉËü¸ú³äֵϵͳ½øÐÐsocketͨѶ£¬×Ô¶¯Íê³É³äÖµµÄ²âÊÔ¹¤×÷¡£
¿´´úÂë°É£º
import socket,thread
def Communicate(s):
phoneno = raw_input("Please input your PhoneNo.:(Pu ......
E-mailÖ÷ÒªÓÉÓʼþÍ·ºÍÓʼþÌåÁ½²¿·Ö×é³É¡£
ÓʼþÍ·ÖеÄÄÚÈݺÍÎÒÃǼÄÐÅʱдÔÚÐÅ·âÉϵÄÄÚÈÝ´óͬСÒ⣬µ±È»ÕâÀïÒ²°üº¬Á˺ܶà·¹ýµÄ“Óʾ֔µÄÐÅÏ¢ÁË¡£
ÓʼþÌåÖеÄÄÚÈݾÍÊÇÎÒÃÇдµÄÐÅ»òÕß°ü¹ü¡£
python×ÔÉí°üº¬ÁËemailÄ£¿é´¦Àí¿ÉÒÔ¿ìËٵĴ¦ÀíE-mailÖеÄÐÅÏ¢
import email
#´ò¿ªÒ»¸öÎļþ
fp = open('email.eml', ' ......