正则表达式练习题

2022年4月27日 06:06 by wst

python小技巧

最近在看一本书(Python 从入门到项目实战),巩固下自己的知识,但发现课后题(P121)的答案(用replace和切片操作实现)竟然不用本章讲的内容(正则替换)。

经过探索给出我的答案。完整如下:

题目:

杰夫•贝佐斯是谁?他是全球最大的网上书店Amazon(亚马逊)的创始人,是美国最具创新力、最令人生畏的企业领导者。2018年3月6日,福布斯2018富豪榜发布,杰夫•贝佐斯以1120亿美元的身价超越另一个程序员比尔•盖茨成为世界首富。他对《福布斯》表示,自己的发展之路才刚刚开始。

编写一个Python程序,将字符串“2018 Amazon Jeff  Bezos 1120”进行操作,实现如下功能:
(1)去掉字符串中的2018字符串,得到字符串:“Amazon  Jeff  Bezos 1120”。
(2)将字符串中的数字提取出来,得到字符串:“20181120”。
(3)将字符串中的数字部分用【】括起来,得到字符串:“【2018】 Amazon Jeff Bezos【1120】”。
(4)去除字符串中所有空格,得到字符串:“2018AmazonJeffBezos1120”。
(5)将字符串中的数字乘以2倍,得到字符串:“4036AmazonBezos2240”。
(6)将字符串“bezos”添加到字符串“要么创新,要么杰夫•贝索斯会替你做”中“杰夫•贝索斯”后面,并用括号括起来。得到字符串:“要么创新,要么杰夫•贝索斯(Jeff  Bezos)会替你做”。

 

我的答案:

import re

def func1():
    str = "2018 Amazon Jeff Bezos 1120"
    print("res1:", re.sub('2018 ', '', str))

    res = re.findall(r'\d+', str)
    print("res2:", ''.join(res))

    res = re.sub(r'(\d+)', '【\\1】', str)
    print("res3:", res)

    res = re.sub(" ", '', str)
    print("res4:", res)

    res = re.findall(r"\d+", str)
    pre = re.sub('\d+', "{}", str)
    print("res5:", pre.format(*[int(e)*2 for e in res]))

    new_str = "要么创新,要么杰夫•贝索斯会替你做"
    res = re.sub("(.*?)杰夫•贝索斯(.*?)", "\\1杰夫•贝索斯(Jeff Bezos)\\2", new_str)
    print("res6:", res)



if __name__ == "__main__":
    func1()

最后一小题的逻辑稍微复杂,这里做个解释:

1. 先得到所有的数字字符串

2. 再把数字字符串替换为占位符;

3. 把得到的数字字符串乘以2之后,填充到占位符里。

 

如果你发现了更好的方法,欢迎评论!

 


Comments(128) Add Your Comment

Mimzws
order atorvastatin 40mg pill <a href="https://lipiws.top/">buy atorvastatin 80mg for sale</a> order generic lipitor
Zcweis
buy ciprofloxacin medication - <a href="https://cipropro.top/">ciprofloxacin 500mg us</a> augmentin 375mg pill
Bhwhpl
buy baycip for sale - <a href="https://metroagyl.top/clindamycin/">buy generic bactrim for sale</a> order amoxiclav generic
Rkuytx
ciplox 500mg sale - <a href="https://septrim.top/chloromycetin/">purchase chloromycetin generic</a> order erythromycin 250mg for sale
Ngybkn
metronidazole order - <a href="https://metroagyl.top/oxytetracycline/">order oxytetracycline 250mg generic</a> azithromycin 500mg cheap
Wqpvof
ivermectin 3 mg tablets for humans - <a href="https://keflexin.top/ceftin/">generic axetil</a> tetracycline brand
Feplkd
valtrex price - <a href="https://gnantiviralp.com/zovirax/">purchase acyclovir online</a> zovirax 400mg sale
Vhbnwr
ampicillin buy online <a href="https://ampiacil.top/amoxil500/">order amoxicillin sale</a> amoxicillin over the counter
Lqnbnx
buy flagyl 400mg for sale - <a href="https://metroagyl.top/cefaclor/">cefaclor over the counter</a> buy azithromycin online cheap
Bnjrff
brand furosemide 100mg - <a href="https://antipathogenc.com/capoten/">capoten 120mg ca</a> purchase captopril generic
Dqrqer
order glucophage - <a href="https://gnantiacidity.com/baycip/">buy cipro 500mg online cheap</a> lincocin 500 mg over the counter
Yhewab
purchase clozapine pills - <a href="https://genonlinep.com/">order clozapine 100mg</a> cost pepcid 40mg
Jurmla
retrovir pills - <a href="https://canadiangnp.com/rulide/">buy biaxsig no prescription</a> buy cheap allopurinol
Uqosqe
clomipramine 25mg usa - <a href="https://antdeponline.com/">buy clomipramine 50mg for sale</a> buy doxepin medication
Teveeg
quetiapine without prescription - <a href="https://gnkantdepres.com/eskalith/">eskalith for sale</a> buy eskalith tablets
Dvphex
hydroxyzine 10mg drug - <a href="https://antdepls.com/buspar/">buspar canada</a> purchase amitriptyline for sale
Lpkdtc
augmentin 375mg over the counter - <a href="https://atbioinfo.com/septra/">buy bactrim 960mg sale</a> baycip sale
Xoehjb
amoxicillin uk - <a href="https://atbioxotc.com/trimox/">generic trimox 500mg</a> oral ciprofloxacin 500mg
Ebvvsk
order zithromax 250mg generic - <a href="https://gncatbp.com/emetronidazole/">flagyl 400mg pill</a> order ciplox 500mg online cheap
Dtwbhb
order cleocin 300mg online cheap - <a href="https://cadbiot.com/cefpodoxime/">order vantin 200mg online</a> buy chloramphenicol cheap
Chjsml
stromectol uk - <a href="https://antibpl.com/">price of stromectol</a> order cefaclor 250mg pill
Kbltqs
ventolin cheap - <a href="https://antxallergic.com/cvallegra/">buy generic allegra 120mg</a> theo-24 Cr pill
Bzskgu
methylprednisolone 16 mg tablet - <a href="https://ntallegpl.com/loratadine/">buy loratadine 10mg without prescription</a> buy azelastine 10ml for sale
Yijtgs
buy desloratadine generic - <a href="https://rxtallerg.com/flixotidespray/">flixotide nasal sprays</a> order albuterol 4mg generic
Jeqvzq
order glyburide 2.5mg online - <a href="https://prodeprpl.com/">glyburide usa</a> buy forxiga 10 mg generic
Nmoxgt
buy glycomet 1000mg generic - <a href="https://arxdepress.com/acarbose50/">purchase acarbose pills</a> acarbose 50mg over the counter
Shrloc
buy generic prandin for sale - <a href="https://depressinfo.com/jardiance25/">jardiance 25mg pills</a> jardiance 25mg generic
Vejbcz
semaglutide 14 mg us - <a href="https://infodeppl.com/ddavpspray/">purchase desmopressin spray</a> where to buy desmopressin without a prescription
Rarbev
lamisil where to buy - <a href="https://treatfungusx.com/fulvicin250mg/">buy griseofulvin 250 mg generic</a> buy griseofulvin online
Cltbea
buy generic famvir - <a href="https://amvinherpes.com/acyclovircream/">acyclovir price</a> valaciclovir 1000mg brand
Xjccmj
order nizoral without prescription - <a href="https://antifungusrp.com/clotrithasone/">buy lotrisone sale</a> sporanox price
Vylfje
order lanoxin 250mg pills - <a href="https://blpressureok.com/">digoxin 250 mg oral</a> furosemide 100mg cost
Wdgapw
purchase lopressor - <a href="https://bloodpresspl.com/telmisartan/">buy telmisartan online cheap</a> adalat over the counter
Wbkitb
hydrochlorothiazide pills - <a href="https://norvapril.com/bisoprolol/">bisoprolol over the counter</a> bisoprolol 5mg price
Jmvprw
nitroglycerin price - <a href="https://nitroproxl.com/indapamide/">order indapamide without prescription</a> buy diovan 80mg pills
Ossomd
simvastatin also - <a href="https://canescholest.com/fenofibrate/">tricor weep</a> atorvastatin complete
Tyqmfn
rosuvastatin feel - <a href="https://antcholesterol.com/ezetimibe/">ezetimibe buy farmer</a> caduet cast
Dzipdh
dapoxetine reduce - <a href="https://promedprili.com/">dapoxetine want</a> cialis with dapoxetine pont
Zcxqak
buy viagra professional hole - <a href="https://edsildps.com/superavana/">avana spend</a> levitra oral jelly online statue
Iughly
brand cialis blaze - <a href="https://probrandtad.com/">brand cialis main</a> penisole sunlight
Tdnsna
cenforce entertain - <a href="https://xcenforcem.com/tadaciptadalafil/">tadacip online cure</a> brand viagra pills change
Inimut
brand cialis mild - <a href="https://probrandtad.com/brandlevitra/">brand levitra otherwise</a> penisole heat
Erpffd
cialis soft tabs online mound - <a href="https://supervalip.com/viagrasuperactive/">viagra super active pills though</a> viagra oral jelly online shudder
Lcbqsg
cialis soft tabs pills afraid - <a href="https://supervalip.com/viagraoraljelly/">viagra oral jelly online hasty</a> viagra oral jelly threaten
Kwsqtj
dapoxetine dot - <a href="https://promedprili.com/viagraplus/">viagra plus darken</a> cialis with dapoxetine peer
Mqzlzy
cenforce scowl - <a href="https://xcenforcem.com/silagrasildenafil/"></a> brand viagra online luck
Cvxhnz
asthma treatment restore - <a href="https://bsasthmaps.com/">asthma medication pace</a> asthma medication roger
Yzpqcu
acne treatment apart - <a href="https://placnemedx.com/">acne medication older</a> acne treatment famous
Fdsyxw
prostatitis treatment leg - <a href="https://xprosttreat.com/">prostatitis treatment rid</a> prostatitis pills menace
Tyclgh
uti treatment post - <a href="https://amenahealthp.com/">uti treatment distract</a> uti treatment dry
Cuonsr
claritin pills gas - <a href="https://clatadine.top/">loratadine boy</a> claritin pills center
Clhqpp
valtrex pills english - <a href="https://gnantiviralp.com/">valtrex online hiss</a> valtrex shower
Xdzokb
priligy alive - <a href="https://prilixgn.top/">priligy expect</a> dapoxetine darken
Kimxhp
claritin pills ready - <a href="https://clatadine.top/">loratadine pry</a> loratadine swallow
Ifuqdu
ascorbic acid fang - <a href="https://ascxacid.com/">ascorbic acid high</a> ascorbic acid expedition
Baacwt
promethazine shatter - <a href="https://prohnrg.com/">promethazine sure</a> promethazine please
Fvinzw
biaxin pills flee - <a href="https://gastropls.com/albendazole400/">albenza pills element</a> cytotec pills brave
Pqgcfs
florinef pills trouser - <a href="https://gastroplusp.com/nexesomeprazole/">nexium pills majesty</a> lansoprazole pills fit
Ghqupi
rabeprazole generic - <a href="https://gastrointesl.com/domperidone/">motilium sale</a> buy generic domperidone
Gemxsv
order dulcolax 5mg - <a href="https://gastroinfop.com/">buy dulcolax 5mg generic</a> buy liv52 10mg
Jhqaxr
zovirax online buy - <a href="https://dupsteron.com/">purchase dydrogesterone for sale</a> order dydrogesterone 10mg for sale
Ldjegc
cotrimoxazole usa - <a href="https://tobmycin.com/">buy cheap tobra</a> generic tobra 10mg
Tzgsch
buy generic griseofulvin 250 mg - <a href="https://gemfilopi.com/">gemfibrozil 300 mg price</a> purchase lopid online cheap
Bojosl
forxiga 10 mg generic - <a href="https://forpaglif.com/">buy dapagliflozin no prescription</a> order precose 50mg sale
Yesuid
order dimenhydrinate 50mg without prescription - <a href="https://actodronate.com/">order risedronate without prescription</a> actonel 35 mg for sale
Mjgjmr
enalapril medication - <a href="https://xalanoprost.com/">buy generic latanoprost for sale</a> cost latanoprost
Dioria
etodolac 600mg sale - <a href="https://cilosetal.com/">buy cilostazol</a> cost cilostazol 100mg
Obzyms
buy generic feldene - <a href="https://rivastilons.com/">buy generic exelon 3mg</a> exelon buy online
Mqipuw
piracetam tablet - <a href="https://nootquin.com/praziquantel/">biltricide ca</a> buy generic sinemet 10mg
Wbnfkv
purchase hydroxyurea generic - <a href="https://hydroydrinfo.com/methocarbamol/">buy methocarbamol</a> methocarbamol medication
Eqhdvq
divalproex 250mg tablet - <a href="https://adepamox.com/topiramate/">cheap topamax 200mg</a> topiramate 200mg usa
Nvfdsi
buy disopyramide phosphate pill - <a href="https://anorpica.com/lamivudine/">buy lamivudine 100mg online cheap</a> buy chlorpromazine 100mg for sale
Virkun
oral aldactone - <a href="https://aldantinep.com/carbamazepine/">cheap epitol tablets</a> buy generic naltrexone
Utrqse
order cyclophosphamide sale - <a href="https://cycloxalp.com/dimenhydrinate/">order dramamine 50 mg sale</a> cheap trimetazidine pill
Dbbhqu
cyclobenzaprine 15mg generic - <a href="https://abflequine.com/donepezil/">buy cheap generic aricept</a> buy vasotec 10mg without prescription
Oaqaih
order ascorbic acid generic - <a href="https://mdacidinfo.com/bromhexine/">bromhexine canada</a> buy compro generic
Ogbnuc
zofran generic - <a href="https://azofarininfo.com/tolterodine/">purchase detrol generic</a> ropinirole 1mg without prescription
Ksptzs
where can i buy durex gel - <a href="https://xalaplinfo.com/latanoprosteyedrops/">buy xalatan</a> buy latanoprost for sale
Obuldd
buy generic rogaine - <a href="https://hairlossmedinfo.com/finasteridehl/">proscar 5mg uk</a> finasteride 5mg canada
Qcijkt
order leflunomide pill - <a href="https://infohealthybones.com/cartidin/">order cartidin pill</a> buy cartidin tablets
Osqcgd
verapamil 240mg drug - <a href="https://infoheartdisea.com/cardizem/">purchase diltiazem pills</a> tenoretic over the counter
Efnzoh
buy generic tenormin 100mg - <a href="https://heartmedinfox.com/">purchase atenolol generic</a> carvedilol price
Hpfpax
purchase gasex pills - <a href="https://herbalinfomez.com/diabecon/">buy generic diabecon</a> buy diabecon tablets
Eoqklg
atorvastatin price - <a href="https://infoxheartmed.com/">cheap atorvastatin pills</a> buy bystolic generic
Ldpemf
lasuna for sale - <a href="https://infoherbalmz.com/himcolin/">buy himcolin pills for sale</a> order himcolin generic
Qdzxnh
noroxin generic - <a href="https://gmenshth.com/confido/">confido price</a> cheap confido online
Mfkfpt
buy finax cheap - <a href="https://finmenura.com/alfuzosin/">alfuzosin 10 mg usa</a> alfuzosin oral
Nmgsll
hytrin 5mg oral - <a href="https://hymenmax.com/dapoxetine/">buy priligy without prescription</a> buy cheap dapoxetine
Vtxciy
speman for sale online - <a href="https://spmensht.com/finasteride/">purchase finasteride for sale</a> buy finasteride medication
Sfzquz
buy oxcarbazepine 600mg online cheap - <a href="https://trileoxine.com/">buy oxcarbazepine without prescription</a> levothroid ca
Xajsqk
purchase cyclosporine generic - <a href="https://asimusxate.com/gloperba/">purchase colchicine pills</a> colchicine where to buy
Sisjux
buy deflazacort pills for sale - <a href="https://lazacort.com/">buy deflazacort medication</a> buy brimonidine eye drops
Ghkfqh
buy duphalac bottles - <a href="https://duphalinfo.com/">lactulose bottless</a> buy betahistine 16mg without prescription
Wfhpgf
buy generic besivance - <a href="https://besifxcist.com/">purchase besivance without prescription</a> buy sildamax without a prescription
Pmgdmu
order gabapentin sale - <a href="https://aneutrin.com/">purchase neurontin generic</a> purchase sulfasalazine pill
Nhhlif
purchase benemid pills - <a href="https://bendoltol.com/">oral benemid 500mg</a> buy cheap tegretol
Kdjibk
order celecoxib 200mg pills - <a href="https://celespas.com/">buy celecoxib paypal</a> buy indocin 75mg
Knbrcp
colospa ca - <a href="https://coloxia.com/pletal/">order cilostazol 100 mg pills</a> order pletal 100mg sale
Difigi
buy generic diclofenac - <a href="https://dicloltarin.com/aspirin/">cheap aspirin</a> aspirin order
Aypldb
purchase rumalaya for sale - <a href="https://rumaxtol.com/shallaki/">cheap shallaki</a> order generic amitriptyline 10mg
Moolvj
mestinon pills - <a href="https://mestonsx.com/azathioprine/">order azathioprine without prescription</a> buy imuran 50mg generic
Uzpdgz
voveran for sale online - <a href="https://vovetosa.com/tisosorbide/">isosorbide us</a> buy nimodipine pill
Fhyfgl
ozobax uk - <a href="https://baclion.com/">brand baclofen 25mg</a> order feldene 20 mg for sale
Xubqnn
buy meloxicam generic - <a href="https://meloxiptan.com/rtrizatriptan/">ketorolac without prescription</a> buy toradol 10mg online cheap
Kmakmc
periactin 4 mg without prescription - <a href="https://periheptadn.com/">buy cyproheptadine 4 mg without prescription</a> tizanidine 2mg uk
Dybdpt
buy trihexyphenidyl generic - <a href="https://voltapll.com.com/">trihexyphenidyl buy online</a> buy diclofenac gel online cheap
Xdmokk
buy generic omnicef - <a href="https://omnixcin.com/">buy omnicef 300mg pill</a> generic cleocin
Harjvs
accutane 40mg sale - <a href="https://aisotane.com/">order isotretinoin 40mg generic</a> deltasone 5mg for sale
Qyhekx
purchase prednisone online cheap - <a href="https://apreplson.com/">deltasone sale</a> buy elimite without a prescription
Lguujh
acticin over the counter - <a href="https://actizacs.com/asbenzac/">where can i buy benzoyl peroxide</a> tretinoin online buy
Fhkmwl
betamethasone online buy - <a href="https://betnoson.com/awdifferin/">adapalene drug</a> purchase benoquin cream
Aincaq
buy generic flagyl - <a href="https://ametronles.com/">metronidazole generic</a> buy cenforce pills for sale
Qvykni
buy augmentin generic - <a href="https://baugipro.com/">amoxiclav price</a> levoxyl online order
Yuppdg
buy generic clindamycin - <a href="https://clinycinpl.com/">cheap clindamycin</a> indomethacin 50mg capsule
Qooxmi
buy cheap generic losartan - <a href="https://acephacrsa.com/">cephalexin 500mg pills</a> how to get cephalexin without a prescription
Zpeowj
eurax canada - <a href="https://aeuracream.com/caczone/">purchase aczone pills</a> aczone price
Hgthux
order modafinil 100mg generic - <a href="https://sleepagil.com/phenergan/">promethazine price</a> cheap melatonin 3mg
Hjzuls
buy bupropion 150 mg generic - <a href="https://bupropsl.com/ayurslim/">buy generic ayurslim online</a> cheap generic shuddha guggulu
Urdjya
prometrium 200mg pills - <a href="https://apromid.com/clomiphene/">buy clomid 50mg sale</a> fertomid sale
Kovrvz
xeloda 500mg tablet - <a href="https://xelocap.com/">how to buy xeloda</a> danazol 100mg uk
Fupdrh
fosamax 70mg us - <a href="https://pilaxmax.com/">order fosamax pills</a> order provera 10mg without prescription
Jawkdl
norethindrone cheap - <a href="https://norethgep.com/yasmin/">yasmin canada</a> yasmin online
Azmuco
oral cabergoline 0.5mg - <a href="https://adostilin.com/alesse/">order alesse for sale</a> purchase alesse pill
Kefhbx
order estrace sale - <a href="https://festrolp.com/letrozole/">order letrozole 2.5 mg pill</a> anastrozole 1 mg pill
Tgnqzt
バイアグラ おすすめ - <a href="https://jpedpharm.com/tadalafil/">シアリス 値段</a> タダラフィル 個人輸入 おすすめ
Vkebkc
プレドニン は通販での購 - <a href="https://jpaonlinep.com/jazithromycin/">ジスロマック 薬局で買える</a> 正規品ジスロマック錠の正しい処方
Ogwnao
正規品プレドニン錠の正しい処方 - <a href="https://jpanfarap.com/jpadoxycycline/">ドキシサイクリン 市販 おすすめ</a> イソトレチノイン処方
Gahxlu
eriacta overhead - <a href="https://eriagra.com/dapcalisb/">apcalis echo</a> forzest consult