B
B
Bogdan Karpov2018-05-12 13:59:10
Python
Bogdan Karpov, 2018-05-12 13:59:10

How to remember regular expressions in python3?

Let's say in php I want to add an @ sign to all words, then I write like this:

echo preg_replace('#(\w+)#si', '\@\\1', 'text text text');

I use ( ) for this - as what I want to remember and \\1 - as a variable, how to do this in python ??

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
SagePtr, 2018-05-12
@SagePtr

Parentheses will help you, the code example is quick:

>>> import re
>>> re.sub(r"(\w+)", "@\\1", "This is a test")
'@This @is @a @test'

N
Nick Sdk, 2018-05-12
@lidacriss

almost the same
https://repl.it/repls/PureVisibleAssembler

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question