piglatin.php源代码分析

<script>function StorePage(){d=document;t=d.selection?(d.selection.type!='None'?d.selection.createRange().text:''):(d.getSelection?d.getSelection():'');void(keyit=window.open('http://www.365key.com/storeit.aspx?t='+escape(d.title)+'&u='+escape(d.location.href)+'&c='+escape(t),'keyit','scrollbars=no,width=475,height=575,left=75,top=20,status=no,resizable=yes'));keyit.focus();}</script>

"""Convert text to Pig Latin

This program is part of "Dive Into Python", a free Python book for

experienced programmers. Visit http://diveintopython.org/ for the

latest version.

"""

__author__ = "Mark Pilgrim ([email protected])"

__version__ = "$Revision: 1.2 $"

__date__ = "$Date: 2004/05/05 21:57:19 $"

__copyright__ = "Copyright (c) 2002 Mark Pilgrim"

__license__ = "Python"

import re

def _wordToPigLatin(match):

word = match.group()

#获得匹配的所有结果

consonants = match.group(1)

#获得第一个结果

restOfWord = match.group(2)

#获得第二个结果

# put consonants after rest of word, and add "ay"

result = "%s%say" % (restOfWord, consonants)

#重新组织字符串

# if word was all uppercase, make result uppercase

if word == word.upper():

result = result.upper()

#如果word的内容不是所有的都是大写,那么让result全部大写

# if word was capitalized, make result capitalized

elif word == word.capitalize():

result = result.capitalize()

%否则让result第一个字母大写

return result

def pigLatin(source):

pattern = re.compile(r'\b([bcdfghjklmnpqrstvwxyz]*)(\w+)\b', re.IGNORECASE)

%获得正则表达式

return pattern.sub(_wordToPigLatin, source)

你可能感兴趣的:(c,PHP,python,正则表达式,REST)