0

I'm trying to find the entire word exactly using regex but have the word i'm searching for be a variable value coming from user input. I've tried this:

regex = r"\b(?=\w)" + re.escape(user_input) + r"\b"
if re.match(regex, string_to_search[i], re.IGNORECASE):
      <some code>...

but it matches every occurrence of the string. It matches "var"->"var" which is correct but also matches "var"->"var"iable and I only want it to match "var"->"var" or "string"->"string"

Input: "sword"

String_to_search = "There once was a swordsmith that made a sword"

Desired output: Match "sword" to "sword" and not "swordsmith"

10
  • 4
    Please post your input and desired output. Commented Aug 15, 2017 at 15:52
  • Well, \bvar\b cannot match var in variable. Why are you using re.match? If you want to match user input as a whole string, you may use regex = '{}$'.format(re.escape(user_input)) and then use re.match(). Else, if you need to really just find var as a whole word inside a larger string, you will need re.search with \bvar\b regex. Commented Aug 15, 2017 at 15:52
  • Doesn't python have a non regex function like a substring search ? Commented Aug 15, 2017 at 15:52
  • If python supported conditionals you could wrap it into conditional boundary's (?(?=\w)\b)(?: your literal )(?(?<=\w)\b) And this \b(?=\w) forces the literal to start with a \w Commented Aug 15, 2017 at 15:58
  • @sln yeah but it will find any substring including "var" in variable which i don't want. I'm going to try the .format or re.search and conditional Commented Aug 15, 2017 at 16:06

2 Answers 2

3

You seem you want to use a pattern that matches an entire string. Note that \b word boundary is needed when you wan to find partial matches. When you need a full string match, you need anchors. Since re.match anchors the match at the start of string, all you need is $ (end of string position) at the end of the pattern:

regex = '{}$'.format(re.escape(user_input))

and then use

re.match(regex, search_string, re.IGNORCASE)
Sign up to request clarification or add additional context in comments.

Comments

1

You can try re.finditer like that:

>>> import re
>>> user_input = "var"
>>> text = "var variable var variable"
>>> regex = r"(?=\b%s\b)" % re.escape(user_input)
>>> [m.start() for m in re.finditer(regex, text)]
[0, 13]

It'll find all matches iteratively.

2 Comments

Where is the output saved to in the loop? Because i would like to break on the first finding and say if it was found, do this, else, do that.
@E.Oregel then just put this into the loop like this: for m in re.finditer(regex, text): and to inside the loop what you want.

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.