Python - Izohlar orasidagi satrlarni kiriting

Har bir sonni o'z raqamining o'zi bilan, lekin tirnoq bilan almashtirish kerak:

str = 'This is number 1 and this is number 22'

Natija:

str= 'This is number "1" and this is number "22"'

Bilsam, bu regexdan foydalanib, har bir sonni boshqa magistral bilan almashtirish uchun foydalanaman:

str = re.sub("\d", "x", str)

Lekin bu menga beradi:

str= 'This is number x and this is number xx'

O'zaro mos keladigan raqam bilan qanday o'zgartirish mumkin?

3
FYI kodi str deb o'zgartirilganda hech qachon str dan foydalanmaslik kerak. Python ma'lumot turiga kiritilgan.
qo'shib qo'ydi muallif Alex Fung, manba

6 javoblar

Siz mos keladigan guruhlarga murojaat qilish uchun \ 1 tuzilishini foydalanishingiz mumkin.

>>> re.sub(r"(\d+)", r'"\1"', "This is number 1 and this is number 22")
'This is number "1" and this is number "22"'

Tarkaridan qochish kerak bo'lmagan xom freymlarni (r bilan oldindan) eslatib o'ting - siz ham mos keladigan naqsh uchun xom qatorni ishlatishingiz kerak, aks holda \ d kelajakda qochish.

Bunga qo'shimcha ravishda, (\ d +) naqsh bir satrda bir qator raqamlar bilan mos keladi - bu holda, kabi bir qator bu raqam "1" va bu raqam "2" "2" ishlab chiqariladi.

5
qo'shib qo'ydi

You can use a backreference to the previously matched content in the replacement pattern. Syntax is \g, where Number is the number of the captured group or 0 for the whole match.

As you want to quote contiguous numbers as one and not number by number, you need to use  \d+ as pattern, not just \d

To'liq kod:

import re
str = 'This is number 1 and this is number 22'
pat = r'\d+'
repl = r'"\g<0>"'
print(re.sub(pat, repl, str))

See http://ideone.com/x5uLte

1
qo'shib qo'ydi

Siz oddiygina narsalar uchun oddiy oddiy so'zlar kerak emas:

>>> words = 'This is number 1 and this is number 22'
>>> " ".join('"{}"'.format(x) if x.isdigit() else x for x in words.split())
'This is number "1" and this is number "22"'
1
qo'shib qo'ydi

Buni ko'ring,

import re
str = 'This is number 1 and this is number 22'
for s in str.split():
    if s.isdigit():
        str=re.sub(s, '"'+s+'"', str)
print str
0
qo'shib qo'ydi

Raqamingizni shunday ushlashingiz kerak:

(\d+)

And the replace with back reference (+your quotes around it) "$1"

see here: https://regex101.com/r/oXsFX7/1

0
qo'shib qo'ydi

Bu shunday qiladi:

s = re.sub("([0-9]+)",r'"\1"' , s)

Bundan tashqari, str kodi str dan foydalanmaslikka harakat qiling. str o'rnatilgan bir mag'lubiyatga .

0
qo'shib qo'ydi
[0-9] dan foydalanish uchun \ d dan foydalanishning biron bir afzalligi bormi?
qo'shib qo'ydi muallif asongtoruin, manba
Ular ishonadilar, men ham xuddi shunday. Menda ko'proq nazorat mavjud bo'lgani kabi [0-9] ni yoqtiraman. Bundan tashqari, [a-z] kabi belgilar oralig'ini qanday belgilash mumkinligini ko'rsatadi
qo'shib qo'ydi muallif Alex Fung, manba
Python
Python
372 ishtirokchilar

Bu guruh python dasturlash tilini muhokama qilish uchun. Iltimos, o'zingizni hurmat qiling va faqat dasturlash bo'yicha yozing. Botlar mavzusini @botlarhaqida guruhida muhokama qling! FAQ: @PyFAQ Offtopic: @python_uz_offtopic

Python offtopic group !
Python offtopic group !
150 ishtirokchilar

@python_uz gruppasining offtop gruppasi. offtop bo'lsa ham reklama mumkin emas ) Boshqa dasturlash tiliga oid gruppalar @languages_programming