有没有办法改变从regex.search()函数返回的匹配对象?

2024-06-26 14:24:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我是一个新的编码和制定了一个项目,我自己开始学习,但我不能绕过这个问题。我正在尝试制作一个小工具来转换剪贴板上的内容(现在我只使用一个名为spam的字符串),这样句子就以大写字母开头,其中'i'也是大写字母,所以'i'。你知道吗

所以我想做的是,找到一个有(''的匹配, '? ' 或者“我”)从那里开始。你知道吗

spam='this is a string which i want to correct. as you can see.'
def capital(lists):            #finds out where to change the text
    dotRegex=re.compile(r'\. ')
    questionRegex=re.compile(r'\? ')
    iRegex=re.compile(r' i ')
    mo1=dotRegex.search(lists)
    mo2=questionRegex.search(lists)
    mo3=iRegex.search(lists)
    if mo1:
        (lists(mo1.start()+2)).upper()
    if mo2:
        (lists(mo1.start()+2)).upper()
    if mo3:
        mo3.upper()
capital(spam)

返回错误:

"(lists(mo1.start()+2)).upper() TypeError: 'str' object is not callable"

我要做的是找到有mo的地方,向右走2个不精确的地方,然后把那里的改成大写。有什么办法吗?当然search()函数只返回1mo所以我的问题是:当有多个匹配的对象时,有没有办法解决这个问题并将它们全部更改?我知道findall()存在,但你怎么能在这里使用它呢?你知道吗

不管怎样,我想从任何人的帮助,我很抱歉,如果这个代码伤害观看。你知道吗


Tags: toresearchifis大写字母spamupper
1条回答
网友
1楼 · 发布于 2024-06-26 14:24:37

代码中有几个错误(或尴尬)。你知道吗

下面是一个快速代码回顾:

import re

spam = 'this is a string which i want to correct. as you can see.'

def capital(lists):
    # finds out where to change the text.
    dot_regex = re.compile(r'\. ')
    question_regex = re.compile(r'\? ')
    i_regex = re.compile(r' i ')
    mo1 = dot_regex.search(lists)
    mo2 = question_regex.search(lists)
    mo3 = i_regex.search(lists)
    if mo1:
        lists[mo1.start() + 2].upper()
    if mo2:
        lists[mo1.start() + 2].upper()
    if mo3:
        mo3.group().upper()

capital(spam)
  1. 根据PEP8 conventions,变量应该用snake大小写。所以我用dot_regex代替dotRegex
  2. 因为不在函数中修改它,所以也可以对RegEx使用模块级变量(常量):例如:DOT_REGEX。你知道吗
  3. 在二进制运算符:mo1 = ...周围放置空格。你知道吗

在Python中,字符串索引/切片使用[]运算符,因此将lists(mo1.start() + 2)替换为lists[mo1.start() + 2]。这里,语法lists(...)是一个函数调用。你知道吗

记住,在Python中,strings are immutable:所以不能修改它,必须创建一个副本。你知道吗

foo = "string"
foo[2] = "l"  # <- TypeError: 'str' object does not support item assignment

回答您的问题:不,您不能修改字符串,但是您可以使用search/replace with RegEx来执行您想要的操作。你知道吗

以下是dot_regex的详细说明:

import re

# Search the first letter after a dot (or after the begining)
dot_regex = re.compile(r"(^|\. )(.)")

def my_upper(mo):
    """ Keep the dot (group #1), turn the letter in uppercase (group #2). """
    return mo.group(1) + mo.group(2).upper()

spam = 'this is a string which i want to correct. as you can see.'
spin = dot_regex.sub(my_upper, spam)
# => This is a string which i want to correct. As you can see.

你可以继续使用其他正则表达式。。。你知道吗

注意:要匹配单个“i”,可以使用r"\bi\b"

# Search a single "i"
i_regex = re.compile(r"\bi\b")

spon = i_regex.sub("I", spin)
print(spon)
# => This is a string which I want to correct. As you can see.

你在做编辑工作,是吗?;—)

您可以将点和问号(以及感叹号)的规则结合起来:

# Search the first letter after a dot/?/! (or after the begining)
mark_regex = re.compile(r"(^|[!?.] )(.)")

spam = 'can you see? this is a string which i want to correct. as you can see! yeh!'
spif = mark_regex.sub(my_upper, spam)
# => Can you see? This is a string which i want to correct. As you can see! Yeh!

教程:Regular Expression HOWTO

相关问题 更多 >