⊗pyPmRESSE 44 of 128 menu

Exceptions within sets in Python regular expressions

You already know that special characters inside [] become regular characters. There are, however, exceptions: if you want square brackets as characters inside [ ] - you need to escape them with a backslash. For example, in the following code, the search pattern looks like this: there is a square bracket between the x's:

txt = 'x]x xax x[x x1x' res = re.sub('x[\[\]]x', '!', txt) print(res)

As a result, the following will be written to the variable:

'! xax ! x1x'

Given a string:

txt = 'x[]z x[[]]z x()z'

Write a regular expression that will find all words with the pattern: letter 'x', then square brackets any number of times, then letter 'z'.

Given a string:

txt = 'x[]z x{}z x.z x()z x([])z'

Write a regular expression that will find all words with the pattern: letter 'x', then any number of any brackets, then letter 'z'.

English
AfrikaansAzərbaycanБългарскиবাংলাБеларускаяČeštinaDanskDeutschΕλληνικάEspañolEestiSuomiFrançaisहिन्दीMagyarՀայերենIndonesiaItaliano日本語ქართულიҚазақ한국어КыргызчаLietuviųLatviešuМакедонскиMelayuမြန်မာNederlandsNorskPolskiPortuguêsRomânăРусскийසිංහලSlovenčinaSlovenščinaShqipСрпскиSrpskiSvenskaKiswahiliТоҷикӣไทยTürkmenTürkçeЎзбекOʻzbekTiếng Việt
We use cookies for website operation, analytics, and personalization. Data processing is carried out in accordance with the Privacy Policy.
accept all customize decline