从行首和行尾或仅行尾删除字符 [英] Remove characters from beginning and end or only end of line
问题描述
我想使用正则表达式从字符串中删除一些符号,例如:
==
(出现在行首和行尾),
*
(仅在行首).
def some_func():clean = re.sub(r'= {2,}', '', clean) #在行首和行尾删除 2 个或多个 =.clean = re.sub(r'^\* {1,}', '', clean) #删除一行开头的 1 个或多个 *.
我的代码有什么问题?好像表述有误.如果某个字符/符号位于行首或行尾(出现一次或多次),我该如何删除它?
如果只想删除开头和结尾的字符,可以使用 string.strip()
方法.这将给出一些这样的代码:
strip
方法从字符串的开头和结尾删除参数中给出的字符,ltrip
只从开头删除它们,而 rstrip
仅从末尾删除它们.
如果你真的想使用正则表达式,它们看起来像这样:
clean = re.sub(r'(^={2,})|(={2,}$)', '', clean)clean = re.sub(r'^\*+', '', clean)
但是恕我直言,使用 strip
/lstrip
/rstrip
将最适合您想要做的事情.
根据尼克的建议,这里有一个解决方案,可以在一行中完成所有这些:
clean = clean.lstrip('*').strip('=')
(一个常见的错误是认为这些方法按照它们在参数中给出的顺序删除字符,实际上,参数只是要删除的字符序列,无论它们的顺序是什么,这就是为什么 .strip('=')
会从开头和结尾删除每个 '=' 和 ' ',而不仅仅是字符串 '='.)
I want to remove some symbols from a string using a regular expression, for example:
==
(that occur both at the beginning and at the end of a line),
*
(at the beginning of a line ONLY).
def some_func():
clean = re.sub(r'= {2,}', '', clean) #Removes 2 or more occurrences of = at the beg and at the end of a line.
clean = re.sub(r'^\* {1,}', '', clean) #Removes 1 or more occurrences of * at the beginning of a line.
What's wrong with my code? It seems like expressions are wrong. How do I remove a character/symbol if it's at the beginning or at the end of the line (with one or more occurrences)?
If you only want to remove characters from the beginning and the end, you could use the string.strip()
method. This would give some code like this:
>>> s1 = '== foo bar =='
>>> s1.strip('=')
' foo bar '
>>> s2 = '* foo bar'
>>> s2.lstrip('*')
' foo bar'
The strip
method removes the characters given in the argument from the beginning and the end of the string, ltrip
removes them from only the beginning, and rstrip
removes them only from the end.
If you really want to use a regular expression, they would look something like this:
clean = re.sub(r'(^={2,})|(={2,}$)', '', clean)
clean = re.sub(r'^\*+', '', clean)
But IMHO, using strip
/lstrip
/rstrip
would be the most appropriate for what you want to do.
Edit: On Nick's suggestion, here is a solution that would do all this in one line:
clean = clean.lstrip('*').strip('= ')
(A common mistake is to think that these methods remove characters in the order they're given in the argument, in fact, the argument is just a sequence of characters to remove, whatever their order is, that's why the .strip('= ')
would remove every '=' and ' ' from the beginning and the end, and not just the string '= '.)
这篇关于从行首和行尾或仅行尾删除字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!