How to use regex to disallow non-digts but allow a dot in Python? -


i new regex , trying remove non-digts keep dot (.) of string:

x = ['abcd, efgh ', ' 20.9&dog; ', ' ijklm />'] 

so far have tried following:

>>> x = re.sub("\d", "", x) 209 

however trying following outcome:

20.9 

thanks.

you want inverted character class:

re.sub(r"[^\d.]", "", x) 

note [^0-9.] , [^\d.] not same, because \d matches many more characters 0123456789:

>>> print(textwrap.fill( ...    "".join(x x in (chr(y) y in range(0x110000)) ...            if re.match(r"\d", x)), ...    break_long_words=true, width=10)) 0123456789 ٠١٢٣٤٥٦٧٨٩ ۰۱۲۳۴۵۶۷۸۹ ߀߁߂߃߄߅߆߇߈߉ ०१२३४५६७८९ ০১২৩৪৫৬৭৮৯ ੦੧੨੩੪੫੬੭੮੯ ૦૧૨૩૪૫૬૭૮૯ ୦୧୨୩୪୫୬୭୮୯ ௦௧௨௩௪௫௬௭௮௯ ౦౧౨౩౪౫౬౭౮౯ ೦೧೨೩೪೫೬೭೮೯ ൦൧൨൩൪൫൬൭൮൯ ෦෧෨෩෪෫෬෭෮෯ ๐๑๒๓๔๕๖๗๘๙ ໐໑໒໓໔໕໖໗໘໙ ༠༡༢༣༤༥༦༧༨༩ ၀၁၂၃၄၅၆၇၈၉ ႐႑႒႓႔႕႖႗႘႙ ០១២៣៤៥៦៧៨៩ ᠐᠑᠒᠓᠔᠕᠖᠗᠘᠙ ᥆᥇᥈᥉᥊᥋᥌᥍᥎᥏ ᧐᧑᧒᧓᧔᧕᧖᧗᧘᧙ ᪀᪁᪂᪃᪄᪅᪆᪇᪈᪉ ᪐᪑᪒᪓᪔᪕᪖᪗᪘᪙ ᭐᭑᭒᭓᭔᭕᭖᭗᭘᭙ ᮰᮱᮲᮳᮴᮵᮶᮷᮸᮹ ᱀᱁᱂᱃᱄᱅᱆᱇᱈᱉ ᱐᱑᱒᱓᱔᱕᱖᱗᱘᱙ ꘠꘡꘢꘣꘤꘥꘦꘧꘨꘩ ꣐꣑꣒꣓꣔꣕꣖꣗꣘꣙ ꤀꤁꤂꤃꤄꤅꤆꤇꤈꤉ ꧐꧑꧒꧓꧔꧕꧖꧗꧘꧙ ꧰꧱꧲꧳꧴꧵꧶꧷꧸꧹ ꩐꩑꩒꩓꩔꩕꩖꩗꩘꩙ ꯰꯱꯲꯳꯴꯵꯶꯷꯸꯹ 0123456789 𐒠𐒡𐒢𐒣𐒤𐒥𐒦𐒧𐒨𐒩 𑁦𑁧𑁨𑁩𑁪𑁫𑁬𑁭𑁮𑁯 𑃰𑃱𑃲𑃳𑃴𑃵𑃶𑃷𑃸𑃹 𑄶𑄷𑄸𑄹𑄺𑄻𑄼𑄽𑄾𑄿 𑇐𑇑𑇒𑇓𑇔𑇕𑇖𑇗𑇘𑇙 𑋰𑋱𑋲𑋳𑋴𑋵𑋶𑋷𑋸𑋹 𑓐𑓑𑓒𑓓𑓔𑓕𑓖𑓗𑓘𑓙 𑙐𑙑𑙒𑙓𑙔𑙕𑙖𑙗𑙘𑙙 𑛀𑛁𑛂𑛃𑛄𑛅𑛆𑛇𑛈𑛉 𑜰𑜱𑜲𑜳𑜴𑜵𑜶𑜷𑜸𑜹 𑣠𑣡𑣢𑣣𑣤𑣥𑣦𑣧𑣨𑣩 𖩠𖩡𖩢𖩣𖩤𖩥𖩦𖩧𖩨𖩩 𖭐𖭑𖭒𖭓𖭔𖭕𖭖𖭗𖭘𖭙 𝟎𝟏𝟐𝟑𝟒𝟓𝟔𝟕𝟖𝟗 𝟘𝟙𝟚𝟛𝟜𝟝𝟞𝟟𝟠𝟡 𝟢𝟣𝟤𝟥𝟦𝟧𝟨𝟩𝟪𝟫 𝟬𝟭𝟮𝟯𝟰𝟱𝟲𝟳𝟴𝟵 𝟶𝟷𝟸𝟹𝟺𝟻𝟼𝟽𝟾𝟿 

i bet didn't know there many variations of hindu-arabic numeral system.

it's worth mentioning in latest 3.x, python's regular expressions not support posix ere named character classes (scroll down "character classes" -- sadly, there no anchor). [^[:digit:].] won't want.


Comments

Popular posts from this blog

javascript - Thinglink image not visible until browser resize -

firebird - Error "invalid transaction handle (expecting explicit transaction start)" executing script from Delphi -

mongodb - How to keep track of users making Stripe Payments -