0
at_the_party = ['bob', 'steve', 'james', 'alfred']
invited_list = ['Mr. bob', 'david', 'james']

First, how do i simply pass the item itterable into re.search, like below?

for item in at_the_party:
    if re.search(item, invited_list):
        print(item, " showed up to the party")
    else:
        print("didnt show up: ", item)

this gives me a generic 'type error'. I would expect to get back the following output:

james

Secondly, I want (i think i need to) pass the variable as what i guess is called string formatting with %s like such:

at_the_party = ['bob', 'steve', 'james', 'alfred']
invited_list = ['Mr. bob', 'david', 'james']



for item in at_the_party:
    if re.search('.*%s.*' %item, invited_list):
        print(item, " showed up to the party")
    else:
        print("didnt show up: ", item)

Is this the correct way to preform a re.search with a variable passed as both the pattern and string?

Oscalation
  • 370
  • 3
  • 15
  • That code most certainly did NOT produce a "generic 'type error'" - it gave you a *specific* error, pointing to a specific place in the code. If you're going to ignore those details, debugging becomes a matter of pure guesswork. – jasonharper Nov 27 '18 at 05:08
  • @jasonharper when i run the first snippet i do get a type error. Specifically i get 'type error - traceback. Then it goes into the code of re. and then another type error stating expected string or bytes like object. I assume this is because im passing a variable (item), which is why I tried the second portion of my code with string formatting and %s – Oscalation Nov 27 '18 at 05:25
  • The error message tells you a line number. The only parameter you're passing in that line that *isn't* a string (and therefore could possibly result in an 'expected string' error) is `invited_list`. Where are you getting the idea that a list is something that you can apply a regex to? – jasonharper Nov 27 '18 at 05:37
  • ah i see. I am not sure where or why i assumed i could pass a list to re.search. would the solution here be to do another enumeration somehow over that second list so that im not passing the entire list to re.search but rather an individual item within that list? – Oscalation Nov 27 '18 at 05:49
  • See [this demo](https://ideone.com/QypSWi), you just need to check if a list item is present inside another list item. – Wiktor Stribiżew Nov 27 '18 at 08:14
  • @WiktorStribiżew any way to do this with regex? I simplified my code above to make it easier to ask my question but my problem requires that i actually use regular expressions. – Oscalation Nov 27 '18 at 12:14
  • @WiktorStribiżew for example - we invited Mr. bob and the person who showed up was just 'bob'. I'd like to match that. – Oscalation Nov 27 '18 at 12:16
  • I guess you are looking for https://ideone.com/SqyiXR – Wiktor Stribiżew Nov 27 '18 at 12:19
  • is there a proper way to pass in my variable item by using either %s or {}.format(item) .. seems like that would be easier syntax. – Oscalation Nov 27 '18 at 12:20
  • regex_search = re.compile('.*{}.*'.format(item)) and then i for i in invited_list if regex_search in at_the_party – Oscalation Nov 27 '18 at 12:22
  • @WiktorStribiżew https://ideone.com/mj0TQr – Oscalation Nov 27 '18 at 12:29
  • I have been there, and let me assure you that building regexps from inside the loop thousands of times will greatly decrease the performance. See [Speed up millions of regex replacements in Python 3](https://stackoverflow.com/questions/42742810/speed-up-millions-of-regex-replacements-in-python-3) You do not need `.*` around the search term as `re.search` allows partial matches. – Wiktor Stribiżew Nov 27 '18 at 12:31
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/184323/discussion-between-oscalation-and-wiktor-stribizew). – Oscalation Nov 27 '18 at 12:43

0 Answers0