0

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 by Ezzaral: Added code tags. Please use them to format any code that you post.

2
Contributors
4
Replies
5
Views
5 Years
Discussion Span
Last Post by amir1990
0

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 by Gribouillis: n/a

0

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 by Ezzaral: Added code tags. Please use them to format any code that you post.

0

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 by Gribouillis: n/a

0

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 topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.