2

I am using python's re module to search in a string for certain words.

For a simple example, I have a list called valid_words and a string called valid_string.

valid_string = "Use Use%"
valid_words = ['Use', 'Use%']

for header in valid_words:
  regex_search = re.search("\\b"+header+"\\b",  valid_string)
  if regex_search:
    print header

However, this only returns Use and not Use%. I think this is because the % in 'Use%' is being treated as a regular expression. Am I correct? Is there a way to get around this?

1 Answer 1

2

The problem here is that you're using word boundaries, however, with your second word 'Use%', the word boundary actually occurs between e and %. For example:

>>> re.search('.\\b','Use%').group(0)
'e'
Sign up to request clarification or add additional context in comments.

3 Comments

Oh, I see. So is there a way to move the boundary to the end of each word in valid_words?
@Fsun -- It's hard to say what is best to do here. It seems like a simple str.split would be what you want, but it's hard to say ...
Thank you. But, str.split actually isn't viable in my situation. For example, an element in valid_words may be "He llo", so the regex search will look for "He llo" in valid_string.

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.