2016-01-22 32 views
1

我正在閱讀「使用Python自動化枯燥的東西」第7章,在項目實踐中:正則表達式版本的條(),這裏是我的代碼(我使用Python 3.X):第7章使用Python自動化枯燥的東西練習項目:正則表達式版本的條()

def stripRegex(x,string): 
import re 
if x == '': 
    spaceLeft = re.compile(r'^\s+') 
    stringLeft = spaceLeft.sub('',string) 
    spaceRight = re.compile(r'\s+$') 
    stringRight = spaceRight.sub('',string) 
    stringBoth = spaceRight.sub('',stringLeft) 
    print(stringLeft) 
    print(stringRight) 

else: 
    charLeft = re.compile(r'^(%s)+'%x) 
    stringLeft = charLeft.sub('',string) 
    charRight = re.compile(r'(%s)+$'%x) 
    stringBoth = charRight.sub('',stringLeft) 
print(stringBoth) 

x1 = '' 
x2 = 'Spam' 
x3 = 'pSam' 
string1 = '  Hello world!!! ' 
string2 = 'SpamSpamBaconSpamEggsSpamSpam' 
stripRegex(x1,string1) 
stripRegex(x2,string2) 
stripRegex(x3,string2) 

這裏是輸出:

Hello world!!! 
     Hello world!!! 
Hello world!!! 
BaconSpamEggs 
SpamSpamBaconSpamEggsSpamSpam 

所以,我帶的正則表達式版本()幾乎與原工作版本。在原始版本中,無論您傳入「垃圾郵件」,「pSam」,「mapS」,「Smpa」,輸出總是「BaconSpamEggs」...所以如何在f這在正則表達式版本?

+0

那麼有沒有關於正則表達式的奧祕。所以你遇到的問題是你已經失去了對代碼流的控制。 – sln

+0

什麼是「原始版本」? –

+0

「原始版本」是本書第6章中介紹的strip()默認方法。例如: spam ='SpamSpamBaconSpamEggsSpamSpam') 其中您輸入:spam.strip('Spam')或spam.strip('Smap')或spam.strip('pSam')...輸出總是:BaconSpamEggs –

回答

0

你可以在正則表達式像這樣的檢查多重角色:

charLeft = re.compile(r'^([%s]+)' % 'abc') 
print charLeft.sub('',"aaabcfdsfsabca") 
>>> fdsfsabca 

甚至更​​好,做一個單一的正則表達式:

def strip_custom(x=" ", text): 
    return re.search(' *[{s}]*(.*?)[{s}]* *$'.format(s=x), text).group(1) 

split_custom('abc', ' aaabtestbcaa ') 
>>> test 
+0

是否應該刪除所有a,b和c?爲什麼a,b,c仍然在輸出中? –

+1

只有在左邊的那個被刪除,類似的方法可以在右邊 – rtemperv

+0

Gotcha - 忘記'strip'只做字符串的結尾 –

0

我切換參數,但是從我的快測試,這似乎工作。我給了它一個可選的參數,默認爲None

def stripRegex(s,toStrip=None): 
    import re 
    if toStrip is None: 
     toStrip = '\s' 
    return re.sub(r'^[{0}]+|[{0}]+$'.format(toStrip), '', s) 

x1 = '' 
x2 = 'Spam' 
x3 = 'pSam' 
string1 = '  Hello world!!! ' 
string2 = 'SpamSpamBaconSpamEggsSpamSpam' 

print(stripRegex(string1)) # 'Hello world!!!' 
print(stripRegex(string1, x1)) # '  Hello world!!! ' 
print(stripRegex(string2, x2)) # 'BaconSpamEggs' 
print(stripRegex(string2, x3)) # 'BaconSpamEggs' 
+0

@DalesVu - Wooh,你在做什麼? –

0

我已經寫了兩個不同的代碼相同: 1路:

import re  
def stripfn(string, c): 
     if c != '': 
      Regex = re.compile(r'^['+ c +']*|['+ c +']*$') 
      strippedString = Regex.sub('', string) 
      print(strippedString) 
     else: 
      blankRegex = re.compile(r'^(\s)*|(\s)*$') 
      strippedString = blankRegex.sub('', string) 
      print(strippedString) 

第二個辦法:

import re 
def stripfn(string, c): 
    if c != '': 
     startRegex = re.compile(r'^['+c+']*') 
     endRegex = re.compile(r'['+c+']*$') 
     startstrippedString = startRegex.sub('', string) 
     endstrippedString = endRegex.sub('', startstrippedString) 
     print(endstrippedString) 
    else: 
     blankRegex = re.compile(r'^(\s)*|(\s)*$') 
     strippedString = blankRegex.sub('', string) 
     print(strippedString) 
0

這似乎工作:

def stripp(text, leftright = None): 
    import re 
    if leftright == None: 
     stripRegex = re.compile(r'^\s*|\s*$') 
     text = stripRegex.sub('', text) 
     print(text) 
    else: 
     stripRegex = re.compile(r'^.|.$') 
     margins = stripRegex.findall(text) 
     while margins[0] in leftright: 
      text = text[1:] 
      margins = stripRegex.findall(text) 
     while margins[-1] in leftright: 
      text = text[:-2] 
      margins = stripRegex.findall(text) 
     print(text) 

mo = ' @@@@@@  ' 
mow = '@&&@#$texttexttext&&^&&&&%%' 
bla = '@&#$^%+' 

stripp(mo) 
stripp(mow, bla) 
0

這裏我的版本:

#!/usr/bin/env python3 

import re 

def strippp(txt,arg=''): # assigning a default value to arg prevents the error if no argument is passed when calling strippp() 
    if arg =='': 
     regex1 = re.compile(r'^(\s+)') 
     mo = regex1.sub('', txt) 
     regex2 = re.compile(r'(\s+)$') 
     mo = regex2.sub('', mo) 
     print(mo) 
    else: 
     regex1 = re.compile(arg) 
     mo = regex1.sub('', txt) 
     print(mo) 

text = '  So, you can create the illusion of smooth motion  ' 
strippp(text, 'e') 
strippp(text) 
相關問題