python - 如何使用re搜索负数?

标签 python

我有一个脚本,打开后会从用户那里获取一个整数并将其保存为结果[1]

然后它打开 myfile.config 并在字符串后搜索整数。该字符串为 locationID=。 因此带有数字的字符串应如下所示:

locationID="34"

或者任何随机数。 它将当前数字替换为结果中的数字。

到目前为止,我的脚本会检查 locationID= 后是否有号码以及是否没有列出号码。

如何让它检查并替换负数?

原文如下:

def replaceid:

    source = "myfile.config"
    newtext = str(results[1])
    with fileinput.FileInput(source, inplace=True, backup='.bak') as file:
        for line in file:
            pattern = r'(?<=locationId=")\d+'  # find 1 or more digits that come
                                              # after the string locationid     

            if re.search(pattern, line):
                sys.stdout.write(re.sub(pattern, newtext, line)) # adds number after locationid
                fileinput.close()
            else:
                sys.stdout.write(re.sub(r'(locationId=)"', r'\1"' + newtext, line)) # use sys.stdout.write instead of "print"
                # using re module to format                                                              
                # adds a location id number after locationid even if there was no number originally there        
                fileinput.close()

这对我不起作用:

def replaceid:

    source = "myfile.config"
    newtext = str(results[1])
    with fileinput.FileInput(source, inplace=True, backup='.bak') as file:
        for line in file:
            pattern = r'(?<=locationId=")\d+'  # find 1 or more digits that come
                                              # after the string locationid     
            patternneg = r'(?<=locationId=-")\d+'

            if re.search(pattern, line):
                sys.stdout.write(re.sub(pattern, newtext, line)) # adds number after locationid
                fileinput.close()

            elif re.search(patternneg, line): # if Location ID has a negative number
                sys.stdout.write(re.sub(patternneg, newtext, line)) # adds number after locationid
                fileinput.close()   

            else:
                sys.stdout.write(re.sub(r'(locationId=)"', r'\1"' + newtext, line)) # use sys.stdout.write instead of "print"
                # using re module to format                                                              
                # adds a location id number after locationid even if there was no number originally there        
                fileinput.close()

最佳答案

由于您没有使用匹配的数字(无论是正数还是负数),您可以更改模式以匹配双引号 .([^"]+) 之间的任何内容。即使它不是数字

pattern = r'(?<=locationId=").([^"]+)'

覆盖空的情况""您可以将模式更改为 .([^"]*) 。另外,您可能希望扩展 locationId 之前/之后有空格的情况的模式。类似 r'(?<=locationId")\s*=\s*.([^"]*)' .

关于python - 如何使用re搜索负数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44460538/

相关文章:

Python 日志记录从多个进程重定向标准输出

python - 直接打开Spyder还是通过Pythonxy打开?

python - captureWarnings 设置为 True 不捕获警告

java - 可以从给定的 protobuf 序列化中提取所有标签号吗?

python - 修改后重新加载模块

python - Django NOT NULL 约束失败 : shop_product. user_id

python - 使用 Python 进行精确的事件同步

python - 以更高的分辨率将 matplotlib 图复制到 QClipboard

python - QT python连接传递参数给函数

python - 在字典中存储逻辑条件并将它们传递给 if 语句