-3

EDIT: So apparently, this is a string interpolation in regex. Thanks for clarifying.

I have a input function that is named omj.

omj 

When I run it, it gives me

"obik"

I then use that output in this regex function

re.findall("\w*obik\w*",dataframe)

I received EXACTLY what I wanted, which is the answer

"Yaobikuni"

Notice that "obik" is in the word, and there is only one match for it. Is there a way to put the input omj in the regex function to get Yaobikuni straightforwardly, or is this the only way it would work?

EDIT: I don't understand why people are downvoting, but I thought I made it clear that omj can be considered as an input string that gives the answer obik.

omj = """obik"""

EDIT2: Thank you for the help @Nick Chapman . I tried this on the first time and I thought it might not have been possible to infuse the input omj on an regex function:

re.findall("\w*"omj"\w*",dataframe)
Sunny League
  • 139
  • 1
  • 8
  • 1
    This is very confusing. Can you clarify your input/output/question? What is `omj`: Is it's a function or an input? – ctwheels Dec 07 '17 at 15:47
  • @ctwheels: It's an input, and I get obik as an output when running it. – Sunny League Dec 07 '17 at 15:50
  • So if it's an input and you get an output of `obik` from it it's a function? How does `omj === obik`? – ctwheels Dec 07 '17 at 15:51
  • @ctwheels: it's a function that gives obik that I equate it to omj. Therefore, you may conclude that omj = """obik""" – Sunny League Dec 07 '17 at 15:56
  • So one could say that `omj` is a function similar to `def omj(): print("obik")`? – ctwheels Dec 07 '17 at 16:00
  • @ctwheels: Yes, and it's an output. If I did omj, dataframe again, my output will still be obik. – Sunny League Dec 07 '17 at 16:10
  • @SunnyLeague I think the goal of the question is a little confusing. If you could start with what exactly you're trying to accomplish you may get better responses. It seems that maybe you're trying to directly pass the output of the function into the regex? – AdamC Dec 07 '17 at 16:14
  • @SunnyLeague you should also use `re.escape()` on `omj` to ensure you're not adding special regex characters to the regex (you'll get incorrect results) – ctwheels Dec 07 '17 at 16:18

2 Answers2

1

The first argument is just a string, so your real question is how to do string interpolation in Python.

How about this:

re.findall("\w*{}\w*".format(omj()), dataframe)
Paulo Scardine
  • 73,447
  • 11
  • 124
  • 153
0

Can't you just do

re.findall("\w*" + omj + "\w*",dataframe)
Nick Chapman
  • 4,402
  • 1
  • 27
  • 41