This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Results with re.sub and re.replace is giving different results
Type: behavior Stage: resolved
Components: Regular Expressions Versions: Python 3.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Mallinath Akkalkot, ezio.melotti, hongweipeng, mrabarnett
Priority: normal Keywords:

Created on 2019-09-03 06:37 by Mallinath Akkalkot, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
Bug_Description_Regular Expression.pptx Mallinath Akkalkot, 2019-09-03 06:44
Messages (2)
msg351066 - (view) Author: Mallinath Akkalkot (Mallinath Akkalkot) Date: 2019-09-03 06:37
Issue is explained in the attached presentation for the below python script.

# PYTHON script
import re

stringPassed = "START, 0.272342208623734, 0.122712611838329\nCIRCL, 0.2739, 0.1175, 0.2644, 0.1175\nCIRCL, 0.2644, 0.108, 0.2644, 0.1175\nLINE, 0.26237716818855, 0.108\nCIRCL, 0.2564, 0.102522934456486, 0.26237716818855, 0.102\nLINE, 0.255041919210401, 0.087\nLINE, 0.255041919210401, 0.072\nCIRCL, 0.239541747114243, -0.0106446715786942, 0.0269671265080016, 0.0720000000000296\nCIRCL, 0.2391, -0.0130000000000008, 0.2456, -0.0130000000000008\nLINE, 0.2391, -0.0350000000000008\nCIRCL, 0.243376868486329, -0.0411080018232587, 0.2456, -0.0350000000000008\nLINE, 0.24865951460066, -0.0430307277670375\nCIRCL, 0.255041919210401, -0.0521457461886608, 0.245341919210401, -0.0521457461886608\nLINE, 0.255041919210401, -0.087\nLINE, 0.2564, -0.102522934456486\nCIRCL, 0.26237716818855, -0.108, 0.26237716818855, -0.102\nLINE, 0.2644, -0.108\nCIRCL, 0.2739, -0.1175, 0.2644, -0.1175\nCIRCL, 0.272342208623734, -0.122712611838329, 0.2644, -0.1175\n"

retVal = re.findall(r"(\w+,\s+)(-\d+\.\d+)|(\d+\.\d+)", stringPassed, re.MULTILINE)

floatNumbersList = []		# List conatins all Folat numbers
for each in retVal:
	for each in each:
		if each != "":floatNumbersList.append(each)

newStringUpdated = stringPassed
for eachVal in floatNumbersList:
	newVal =  str(float(eachVal)*1000)
	retVals = re.sub(eachVal, newVal, newStringUpdated)
#	retVals = newStringUpdated.replace(eachVal, newVal)
	newStringUpdated = retVals

print(newStringUpdated)
msg351068 - (view) Author: hongweipeng (hongweipeng) * Date: 2019-09-03 07:15
It's because ```re.sub('0.072', '72.0', newStringUpdated)``` match '03072' in -0.043'03072'77670375.

`.` can match any character in regular expression.So I don't think that's a issue.
History
Date User Action Args
2022-04-11 14:59:19adminsetgithub: 82197
2019-09-03 07:24:44serhiy.storchakasetstatus: open -> closed
resolution: not a bug
stage: resolved
2019-09-03 07:15:55hongweipengsetnosy: + hongweipeng
messages: + msg351068
2019-09-03 06:44:14Mallinath Akkalkotsetfiles: + Bug_Description_Regular Expression.pptx
2019-09-03 06:43:54Mallinath Akkalkotsetfiles: - Bug_Description_Regular Expression.pptx
2019-09-03 06:37:52Mallinath Akkalkotcreate