2008年5月9日金曜日

正規表現, 文字列の置換



文字列の置換 string.replace()


---
import string
str1 = "http://localhost"
str2 = string.replace(str1, 'http', 'file')
print str1
print str2
---
http://localhost
file://localhost
-----------------------------------

リテラルのバックスラッシュにマッチさせるには、正規表現文字列として
'\\\\'


Python では raw 文字列(raw string)表記を正規表現に利用
先頭に  r  を付加する
p = re.compile(r'([^"])\b((http|https)://[^ \t\n\r<>\(\)&"]+' \
r'[^ \t\n\r<>\(\)&"\.])')


http://www.python.jp/Zope/articles/tips/regex_howto/regex_howto_3
http://reddog.s35.xrea.com/wiki/Python%C8%F7%CB%BA%CF%BF.html Python備忘録


改行文字を取り除くときは、 s[:-1] または s.rstrip('\n')

-------------------------------------------------------------
import re
p = re.compile("ab.", re.I)
result = p.findall("AbdABCAAbb")
print result

p = re.compile("ab.")
print p.sub("xxx", "abcdeaabcde")
print p.sub("xxx", "Abcdeaabcde")

p = re.compile("ab.", re.I)
print p.sub("xxx", "Abcdeaabcde")
print "------------"

url = "aa https://aa.com/ bb"

p = re.compile(r'([^"])\b((http|https)://[^ \t\n\r<>\(\)&"]+' \
r'[^ \t\n\r<>\(\)&"\.])')

m = p.match('aa http://aa.com/ bb' + url + url )
print m
m = p.sub('XXX','aa http://aa.com/ bb' + url + url )
print m
print "------------"

# print m.group(0)

print "-- " + url + " --"
print "->"
print p.sub("xxx", url )

p = re.compile('[a-z]+')
m = p.match('tempo')
print m
print m.group(0)

-----

from google.appengine.api import urlfetch
from elementtree.ElementTree import *
import re

url = 'http://blog.goo.ne.jp/xxxxxx/m/200803'
#content = urlfetch.Fetch(url).content.decode("euc-jp")
content = url.split('/')

print content[0]
print content[1]
print content[2]
print content[3]

content = url.split('/')[2]
print content

Unityでドアの開閉はAnimatorそれともiTween?

Mac Mini M2 の Unity で Sketchup のデータを復元したつづき。 以前、苦労して作成したドアの開閉が動作しないので修復する。 どうやって動かしていたのか、また忘れそうなので記録しておく。             Animator 左右のドア PlaneL,...