Created
December 6, 2016 15:20
-
-
Save segfo/939f9a99500abd6c9b00ea9524ea1147 to your computer and use it in GitHub Desktop.
testRegex.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
import re | |
regexPatt = r".*=([0-9]+)" | |
regexPatt1 = regexPatt[0:regexPatt.find("(")] | |
regexPatt2 = regexPatt[regexPatt.find("("):regexPatt.find(")")+1] | |
regexPatt = regexPatt1+regexPatt2 | |
print "正規表現:"+regexPatt | |
text = "hogefuga=1234\nfugafuga=8882\nsusono" | |
print "[全体のテキスト]\n"+text.replace("\n","\\n") | |
r = re.compile(regexPatt1,re.DOTALL) | |
findNotReplace = r.findall(text)[0] | |
print "[置き換えが起こらない部分(後方参照で一致する以前の部分)]\n"+findNotReplace.replace("\n","\\n") | |
r = re.compile(regexPatt,re.DOTALL) | |
findReplace = r.findall(text)[0] | |
#findReplace = "" if findNotReplace != findNotReplace else findReplace | |
print "[置き換えが起こる部分(後方参照)]\n"+findReplace.replace("\n","\\n") | |
print "[それ以降の部分]" | |
print text.replace(text[text.find(findNotReplace):len(findNotReplace)+len(findReplace)],"").replace("\n","\\n") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment