i have a problem with this code:

s = "johnny"
j = 'j'
y = 'y'
re.search(rj'\w+'+y, s).group

i want to put the j variable instead of the string, with the y variable its ok but with the j i got an error!!!

Edited 4 Years Ago by Ezzaral: Added code tags. Please use them to format any code that you post.

What are you looking for exactly in the string "Johnny" ? What is this .group doing here ? Why is J out of the string in the re.search statement.

Hint: experiment with the re module by using kodos the python debugger (works with python 2, should be installable with easy_install).

Edited 4 Years Ago by Gribouillis: n/a

the j in the statement refers to the variable j='j' like y='y' let me change the code:

s = "johnny"
x = 'j'
z = 'y'
re.search(rz'\w+'+x, s).group

and i want the result to be "johnny", there is no problem with the x variable but with z i got an error message!!!

Edited 4 Years Ago by Ezzaral: Added code tags. Please use them to format any code that you post.

the j in the statement refers to the variable j='j' like y='y' let me change the code:

s = "johnny"
x = 'j'
z = 'y'
re.search(rz'\w+'+x, s).group

and i want the result to be "johnny", there is no problem with the x variable but with z i got an error message!!!

I don't understand, why do you need a regex to find 'Johnny' in 'Johnny' ? Also you can't concatenate a variable name and a string like this.

>>> z = 'J'
>>> z'foo'
  File "<stdin>", line 1
    z'foo'
         ^
SyntaxError: invalid syntax
>>> z + 'foo'
'Jfoo'

In r'foo' it's different, the r has a special meaning (a raw string).

Edited 4 Years Ago by Gribouillis: n/a

no, this is just an example of what i really want to do, please help me with it if you can, thanks for you time.

This article has been dead for over six months. Start a new discussion instead.