Skip to content

Instantly share code, notes, and snippets.

@segfo
Created December 6, 2016 15:20
Show Gist options
  • Save segfo/939f9a99500abd6c9b00ea9524ea1147 to your computer and use it in GitHub Desktop.
Save segfo/939f9a99500abd6c9b00ea9524ea1147 to your computer and use it in GitHub Desktop.
testRegex.py
# -*- 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