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!!!

Recommended Answers

All 4 Replies

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).

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!!!

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).

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.