[英]How can I modify this regex pattern to also remove spaces after a newline \n?
我有一個帶有多余空格的字符串。 我想刪除每行開頭的任何空格,直到顏色。 我還想保留單詞之間的單個空格,如果冒號不位於百分比之前(例如,請查看字符串中的Pastels
)和冒號后的空格數(兩位數為 1 個空格,2 個空格),則不會影響冒號對於個位數)。 到目前為止,我保留了我想要的一切,但我無法擺脫\n
之后的單個空格。
如何以一種模式刪除新行之后和字符串開頭的所有空格?
我希望字符串看起來像這樣: 'Red: 80%\nNavy Blue: 15%\nGreen: 3%\nPastels: Pink, Baby Blue, Lavender: 2%'
my_string = ' Red: 80%\n Navy Blue: 15%\n Green: 3%\n Pastels: Pink, Baby Blue, Lavender: 2%'
my_pattern = re.compile('(?<![:])[ ]{2,}') # match 2 or more spaces unless they follow a colon
# the following:
re.sub(my_pattern, '', my_string)
# returns this:
'Red: 80%\n Navy Blue: 15%\nGreen: 3%\nPastels: Pink, Baby Blue, Lavender: 2%' # Note the number of spaces after the colons and newlines.
# The space before "Navy Blue" is the problem.
# this would give me the desired result, but what pattern would let me do it all within one re.sub() ?
re.sub(my_pattern, '', my_string).replace('\n ', '\n')
# returns this:
'Red: 80%\nNavy Blue: 15%\nGreen: 3%\nPastels: Pink, Baby Blue, Lavender: 2%'
找到了解決方案。 比我最初想象的要簡單得多:
my_pattern = re.compile('(?m)^\s+') # (?m) sets to multiline mode
# ^\s+ matches any whitespace immediately following the start of a line
# a little cleaner way of writing the same thing:
my_pattern = re.compile('^\s+', re.MULTILINE)
# the following:
re.sub(my_pattern, '', my_string)
# returns:
'Red: 80%\nNavy Blue: 15%\nGreen: 3%\nPastels: Pink, Baby Blue, Lavender: 2%'
為了從每行的開頭只刪除水平空白字符,您可以使用
my_pattern = re.compile(r'(?m)^[^\S\r\n]+')
my_pattern = re.compile(r'^[^\S\r\n]+', re.M)
my_pattern = re.compile(r'^[^\S\r\n]+', re.MULTILINE)
# and then use my_pattern.sub:
text = my_pattern.sub('', text)
請注意(?m)
內聯修飾符標志等效於re.M
選項,當您可以在某些鏈接庫中定義的某些函數/方法中使用正則表達式時,它很方便,並且您不想將re
模塊導入只是能夠使用國旗。
詳情:
^
- 行首[^\S\r\n]+
- 任何字符出現一次或多次 ( +
) 但 ( [^...]
是一個否定字符類) CR (回車, \r
), LF (換行, \n
)和非空白字符( \S
)。 因此,這與\s+
相同,其中減去了 LF 和 CR 字符。請參閱正則表達式演示。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.