我有一個帶有多余空格的字串。我想洗掉每行開頭的任何空白,直到顏色。我還想保留單詞之間的單個空格,如果它們不在百分比之前不影響冒號(以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%'
uj5u.com熱心網友回復:
為了從每行的開頭僅洗掉水平空白字符,您可以使用
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 字符相同。
請參閱正則運算式演示。
uj5u.com熱心網友回復:
找到了解決辦法。比我最初想的要簡單得多:
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%'
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/383633.html
上一篇:如何根據分隔符“|”之前的匹配值獲取字串中的第二個值?
下一篇:拆分陣列公式的挑戰
