简体   繁体   English

在Python中查找并替换字符串

[英]Find and replace a string in Python

I have a string like this: 我有一个像这样的字符串:

 line = "Student   =  Small   |1-2|   Student"

I want to replace this line to 我想将此行替换为

 line = "StudentShort  =  Small  |1-2|    StudentShort"

The problem is I don't know wheither first and last words are Student or anything else in string. 问题是我不知道Student或其他任何字符串中的第一个和最后一个单词。 I mean it can be Men , Women , Teacher anything. 我的意思是它可以是MenWomenTeacher

I only know that if there is small in string I have to replace first and last word with that name and short 我只知道如果字符串中有small字母,我必须用该名称替换第一个和最后一个字并且短

Can some one help? 有人可以帮忙吗?

You want to add "Short" to the first and last word of the string...My advice would be to split and then use indexing and then join! 您想要将“Short”添加到字符串的第一个和最后一个单词...我的建议是拆分然后使用索引然后加入!

In [202]: line = "Teacher   =  Small   |1-2|   Student"

In [203]: line = line.split()

In [204]: line[0] += "Short"

In [205]: line[-1] += "Short"

In [206]: line = "  ".join(line)

In [207]: line
Out[207]: 'TeacherShort  =  Small  |1-2|  StudentShort'

I think it would be useful to have this in a function: 我认为在函数中使用它会很有用:

def customize_string(string,add_on):
    if "small" in string:
        line = string.split()
        line[0] += add_on
        line[-1] += add_on
        return "  ".join(line)
    else:
        return string

here is using it to show that it works! 这里用它来表明它的工作原理!

In [219]: customize_string(line,"Short")
Out[219]: 'TeacherShort  =  Small  |1-2|  StudentShort'

Something like this using regex : 使用regex这样的东西:

>>> line = "Student   =  Small   |1-2|   Student"
>>> if re.search(r"\bSmall\b",line):
    print re.sub("^(\w+)|(\w+)$",lambda x:x.group()+"Short",line)
'StudentShort   =  Small   |1-2|   StudentShort'

>>> line = "Men   =  Small   |1-2|   Men"
>>> if re.search(r"\bSmall\b",line):
    print re.sub("^(\w+)|(\w+)$",lambda x:x.group()+"Short",line)
'MenShort   =  Small   |1-2|   MenShort'

An improved version of above code(as suggested by @thg435): 上述代码的改进版本(由@ thg435建议):

def solve(strs, match, word):
    if re.search(r"\b{0}\b".format(match), strs):
         return re.sub(r"(^\w+|\w+$)","\g<0>{0}".format(word), strs)

>>> solve("Men   =  Small   |1-2|   Men", "Small", "Short")
'MenShort   =  Small   |1-2|   MenShort'
>>> solve("Student   =  Small   |1-2|   Student", "Small", "Short")
'StudentShort   =  Small   |1-2|   StudentShort'

You can use the String replace method in python.. 你可以在python中使用String替换方法。

http://docs.python.org/2/library/string.html#string.replace http://docs.python.org/2/library/string.html#string.replace

    string.replace(s, old, new[, maxreplace])
    Return a copy of string s with all occurrences of substring
    old replaced by new. If the optional argument maxreplace is 
    given, the first maxreplace occurrences are replaced.

This selects the "Student" part based on the equals-sign by splitting the string. 这将通过拆分字符串基于等号选择“学生”部分。 then it replaces it using line.replace. 然后它使用line.replace替换它。

line = "Student   =  Small   |1-2|   Student"
name = line.split('=',1)[0].strip()
line = line.replace(name,name+'Short')
print line

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

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