0

I'm trying to search strings with variable using Regular expression operations.
I browsed about it and find this useful code

s = "These are oranges and apples and pears, but not pinapples or .."
r = re.compile(r'\bAND\b | \bOR\b | \bNOT\b', flags=re.I | re.X)
r.findall(s)
['and', 'and', 'not', 'or'] #result

In this code they using exact string value 'AND''OR''NOT'.
What should i do if i have something like this,

a = 'AND'
b = 'OR'

(I'm getting these string values by running a loop)
In this code they are using '| (or)' and re.findall(), what should i do if i need to search both a and b. and use re.search()

Note: I think i need to use r'\bfoo\b' because sometimes my matches will be in this way...'foo.', '(foo)''cod.foo'
because of this i can't use condition likeif a in s: (or) if a and b in s:.
Please give some suggestions to work on this, Thank you.

4
  • Please be more precise in describing your aim. What do you want to extract exactly? What do you mean by string or keywords? You give only one example, that is not enough in order to understand your issue. Commented May 8, 2020 at 13:46
  • @CatalinaChircu ok sorry! in the re.compile(r'\bAND\b | \bOR\b | \bNOT\b', flags=re.I | re.X) is that possible to use variable like 'a', 'b', instead of giving the string value and, or, not Commented May 8, 2020 at 13:54
  • I doubt that you can use variables in regex, no. I asked you what exactly you want to do, because I have the feeling that regex is not the best solution. So, what do you want to extract? Give more examples. Commented May 8, 2020 at 14:06
  • @CatalinaChircu i will explain. i have a excel file(excel1.xls) in that sheet im taking the values of row 1 and row 2(string values). Now i have to search every cell in the other excel sheet(excel2.xls) whether it contains both the row1 and row2 value. sometimes the row1 and row2 values wont be exactly same in excel2.xls. it may contains alphanumeric as prefix or suffix. Commented May 8, 2020 at 14:13

3 Answers 3

1

If you want to search both variables, you can call re.search twice
Something like this...

if((re.search(rf"\b(?=\w){(a)}\b(?!\w)", s, re.IGNORECASE)) and (re.search(rf"\b(?=\w){(b)}\b(?!\w)", s, re.IGNORECASE))): 


Hope it helps

Sign up to request clarification or add additional context in comments.

Comments

1

I might not know what you are going to do, but if your intention is to use variables inside regex, then remember that a regex, before it is send to re.compile, is just a simple text. So you can do with it everything you can do with texts like:

re.compile(f"\\b({a}|{b})\\b")

or in older python:

re.compile("\\b(" + a + "|" + b + ")\\b")

You are not restricted to use r"text" to define regex patterns.

2 Comments

Thank you! (re.search(rf"\b(?=\w){re.escape(a)}|{re.escape(b)}\b(?!\w)", s, re.IGNORECASE)) i tried this and it works fine.
using re.search instead of or (|), is that possible to search both a and b.
0

I think you need something like this, and add flag "case insensitive" too:

pattern = '\b(and|or|not)\b'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.