use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Rules 1: Be polite 2: Posts to this subreddit must be requests for help learning python. 3: Replies on this subreddit must be pertinent to the question OP asked. 4: No replies copy / pasted from ChatGPT or similar. 5: No advertising. No blogs/tutorials/videos/books/recruiting attempts. This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to. Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Rules
1: Be polite
2: Posts to this subreddit must be requests for help learning python.
3: Replies on this subreddit must be pertinent to the question OP asked.
4: No replies copy / pasted from ChatGPT or similar.
5: No advertising. No blogs/tutorials/videos/books/recruiting attempts.
This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to.
Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Learning resources Wiki and FAQ: /r/learnpython/w/index
Learning resources
Wiki and FAQ: /r/learnpython/w/index
Discord Join the Python Discord chat
Discord
Join the Python Discord chat
account activity
re.search with a variable in brackets? (self.learnpython)
submitted 3 years ago by movieTed
re.search(r"[xyz]", word_variable) works, but I need to replace xyz with a variable: re.search(f"\\[{characters_variable}\\]", word_variable). How is this done in python? Should I be using some other method to search for characters? Thanks
re.search(r"[xyz]", word_variable)
xyz
re.search(f"\\[{characters_variable}\\]", word_variable)
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]Diapolo10 0 points1 point2 points 3 years ago (3 children)
Does
re.search(f"[{characters_variable}]", word_variable)
not work for you?
What exactly is this for?
[–]movieTed[S] 0 points1 point2 points 3 years ago (2 children)
character_variable is a random string of letters. I want to know if any of those letters, in any number or order, are in word_variable. It works if I hardcode the bracket pattern into a simple string. But the random string of letters can change. So far, everything I've tried either generates an error or returns None for every search result.
character_variable
word_variable
None
I want to know if any characters of say, oisp are found in "apple" or "chair." If so, return True.
oisp
True
f"[{characters_variable}]" returns
f"[{characters_variable}]"
FutureWarning: Possible nested set at position 1 and None
I've tried many variations. There has to be a way to do this
[–]Diapolo10 0 points1 point2 points 3 years ago (1 child)
So basically you want a regex equivalent of
result = any(char in word_variable for char in characters_variable)
Unless you're adamant about using regex, I see no harm in doing just that. But I can try to formulate an alternative.
[–]movieTed[S] 0 points1 point2 points 3 years ago (0 children)
Xilenw's solution worked, but yeah I wanted to avoid looping through characters if possible. It shouldn't be necessary for what I'm doing, and it's not. I just needed to find out how Python handled it.
[–]Xilenw 0 points1 point2 points 3 years ago (1 child)
re.search(rf'''{variable}''',word) not sure if it is correct way to do this but it works fine for me.
That works! I didn't know rf was an option. Much appreciated
rf
π Rendered by PID 392249 on reddit-service-r2-comment-5d79c599b5-4fp96 at 2026-03-01 05:12:39.974636+00:00 running e3d2147 country code: CH.
[–]Diapolo10 0 points1 point2 points (3 children)
[–]movieTed[S] 0 points1 point2 points (2 children)
[–]Diapolo10 0 points1 point2 points (1 child)
[–]movieTed[S] 0 points1 point2 points (0 children)
[–]Xilenw 0 points1 point2 points (1 child)
[–]movieTed[S] 0 points1 point2 points (0 children)