简体   繁体   English

同时提取和替换字符串中的字母

[英]Extract and replace letters from a string both at the same time

I have a 'string1' with two kinds of positions: the inverse position (identified by position=change) and the direct position (identified by just position=). 我有一个带有两种位置的“ string1”:反向位置(由position = change标识)和直接位置(由position =标识)。 To make myself clear, take for example letters=abcdef and position=change(1-4). 为了使自己清楚,例如使用letter = abcdef和position = change(1-4)。 The letters in between the first and the fourth gap, that are bcd, must be extracted, and letter c must be replaced with the number 1. For reasons not to the case, in the inverse position case, it is always the letter c that must be replaced by digit 1. In the direct position case, the replacing is not performed. 必须提取第一个和第四个间隔之间的字母bcd,并且必须将字母c替换为数字1。由于情况并非如此,在反位情况下,始终是字母c必须用数字1替换。在直接位置情况下,不执行替换。

I need to do this by a loop, and I have written the code I show you here, but it doesn't work. 我需要循环执行此操作,并且已经编写了在此向您展示的代码,但是它不起作用。

string1='*This is an inverse position [position=change(1-4)]in a long sentence\n*This is a direct position [position=2-7] in a long sentence'
import re 
a=re.finditer(r'(\*This).*\]', string1)
for element in a:
    print element.group()

letters='abcdefghijklmno'
b=re.finditer(r'([0-9]+)-([0-9]+)', element.group())
for i in b:
    if element.group().find('change'):
        changedletters=letters[int(i.group(1)) : int(i.group(2))].replace('c', '1')
        print element.group()+changedletters
    else:
        print letters[int(i.group(1)) : int(i.group(2))]

In your code, there is so much wrong: 在您的代码中,有很多错误:

  1. regular expressions are greedy. 正则表达式是贪婪的。 So your finditer finds the first This and the last position. 所以你finditer找到的第一个This和最后的位置。
  2. Your second part of the code is not inside the first loop, so only the last occurence of finditer is processed. 您的代码的第二部分不在第一个循环内,因此仅处理finditer的最后一次出现。
  3. The regular expression is not very fault tolerant 正则表达式不是很容错

Find this: 找到这个:

import re 

LETTERS = 'abcdefghijklmno'

def output_letters(text):
    elements = re.finditer(r'(\*This).*?\[position=(change\()?(\d+)-(\d+)\)?\]', text)
    for element in elements:
        print element.group(0)
        letters = LETTERS[int(element.group(3)):int(element.group(4))]
        if element.group(2):
            letters = letters.replace('c','1')
        print letters


string1='*This is an inverse position [position=change(1-4)]in a long sentence\n*This is a direct position [position=2-7] in a long sentence'
output_letters(string1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM