我试图在搜索过程中使Python的str.partition函数忽略大小写,所以
>>>partition_tuple = 'Hello moon'.partition('hello') ('', 'Hello', ' moon')和
>>>partition_tuple = 'hello moon'.partition('hello') ('', 'hello', ' moon')返回如上所示。
我应该使用正则表达式吗?
谢谢,
编辑:
赦免,我应该更具体。 我想在字符串中找到一个关键字,改变它(通过添加它周围的东西)然后把它放回去。我这样做的计划是制作分区然后改变中间部分然后将它们全部重新组合在一起。
例:
'this is a contrived example'关键字'contrived'将成为:
'this is a <<contrived>> example'即使'人为'拼写为大写'C',我也需要它来执行<< >>
请注意,单词中的任何字母都可以大写,而不仅仅是起始字母。
案件需要保留。
这个问题的另一个独特之处是可以有几个关键字。 事实上,甚至可以有一个关键词。 也就是说,在上面的例子中,关键字可能是“假设的”和“人为的”,在这种情况下,输出需要看起来像:
'this is <<a contrived>> example.'I am trying to make Python's str.partition function ignore case during the search, so
>>>partition_tuple = 'Hello moon'.partition('hello') ('', 'Hello', ' moon')and
>>>partition_tuple = 'hello moon'.partition('hello') ('', 'hello', ' moon')return as shown above.
Should I be using regular expressions instead?
Thanks,
EDIT:
Pardons, I should have been more specific. I want to find a keyword in a string, change it (by adding stuff around it) then put it back in. My plan to do this was make partitions and then change the middle section then put it all back together.
Example:
'this is a contrived example'with keyword 'contrived' would become:
'this is a <<contrived>> example'and I need it to perform the <<>> even if 'contrived' was spelled with a capital 'C.'
Note that any letter in the word could be capitalized, not just the starting one.
The case needs to be preserved.
Another unique point to this problem is that there can be several keywords. In fact, there can even be a key phrase. That is to say, in the above example, the keywords could have been 'a contrived' and 'contrived' in which case the output would need to look like:
'this is <<a contrived>> example.'最满意答案
怎么样
re.split('[Hh]ello', 'Hello moon')这给了
['', ' moon']现在你有了它们,你可以随意把它们放在一起。 它保留了案例。
[编辑] 您可以在一个正则表达式中放置多个关键字(但请注意以下注意事项)
re.split(r'[Hh]ello | moon', 'Hello moon')注意:重新使用匹配的FIRST,然后忽略其余的。
因此,只有在每个目标中都有一个SINGLE关键字时,输入多个关键字才有用。
How about
re.split('[Hh]ello', 'Hello moon')This gives
['', ' moon']Now you have the pieces and you can put them back together as you please. And it preserves the case.
[Edit] You can put multiple keywords in one regex (but read caution below)
re.split(r'[Hh]ello | moon', 'Hello moon')Caution: re will use the FIRST one that matches and then ignore the rest.
So, putting in multiple keywords is only useful if there is a SINGLE keyword in each target.
有没有办法让python str.partition忽略大小写?(Is there a way to make python str.partition ignore case?)我试图在搜索过程中使Python的str.partition函数忽略大小写,所以
>>>partition_tuple = 'Hello moon'.partition('hello') ('', 'Hello', ' moon')和
>>>partition_tuple = 'hello moon'.partition('hello') ('', 'hello', ' moon')返回如上所示。
我应该使用正则表达式吗?
谢谢,
编辑:
赦免,我应该更具体。 我想在字符串中找到一个关键字,改变它(通过添加它周围的东西)然后把它放回去。我这样做的计划是制作分区然后改变中间部分然后将它们全部重新组合在一起。
例:
'this is a contrived example'关键字'contrived'将成为:
'this is a <<contrived>> example'即使'人为'拼写为大写'C',我也需要它来执行<< >>
请注意,单词中的任何字母都可以大写,而不仅仅是起始字母。
案件需要保留。
这个问题的另一个独特之处是可以有几个关键字。 事实上,甚至可以有一个关键词。 也就是说,在上面的例子中,关键字可能是“假设的”和“人为的”,在这种情况下,输出需要看起来像:
'this is <<a contrived>> example.'I am trying to make Python's str.partition function ignore case during the search, so
>>>partition_tuple = 'Hello moon'.partition('hello') ('', 'Hello', ' moon')and
>>>partition_tuple = 'hello moon'.partition('hello') ('', 'hello', ' moon')return as shown above.
Should I be using regular expressions instead?
Thanks,
EDIT:
Pardons, I should have been more specific. I want to find a keyword in a string, change it (by adding stuff around it) then put it back in. My plan to do this was make partitions and then change the middle section then put it all back together.
Example:
'this is a contrived example'with keyword 'contrived' would become:
'this is a <<contrived>> example'and I need it to perform the <<>> even if 'contrived' was spelled with a capital 'C.'
Note that any letter in the word could be capitalized, not just the starting one.
The case needs to be preserved.
Another unique point to this problem is that there can be several keywords. In fact, there can even be a key phrase. That is to say, in the above example, the keywords could have been 'a contrived' and 'contrived' in which case the output would need to look like:
'this is <<a contrived>> example.'最满意答案
怎么样
re.split('[Hh]ello', 'Hello moon')这给了
['', ' moon']现在你有了它们,你可以随意把它们放在一起。 它保留了案例。
[编辑] 您可以在一个正则表达式中放置多个关键字(但请注意以下注意事项)
re.split(r'[Hh]ello | moon', 'Hello moon')注意:重新使用匹配的FIRST,然后忽略其余的。
因此,只有在每个目标中都有一个SINGLE关键字时,输入多个关键字才有用。
How about
re.split('[Hh]ello', 'Hello moon')This gives
['', ' moon']Now you have the pieces and you can put them back together as you please. And it preserves the case.
[Edit] You can put multiple keywords in one regex (but read caution below)
re.split(r'[Hh]ello | moon', 'Hello moon')Caution: re will use the FIRST one that matches and then ignore the rest.
So, putting in multiple keywords is only useful if there is a SINGLE keyword in each target.
发布评论