0

I have been trying various solutions all yesterday, before I hung it up and went to bed. After coming back today and taking another look at it... I still cannot understand what is wrong with my regex statement.

I am trying to search my inventory based on a simple name and return an item index and the amount of that item that I have.

for instance, in my inventory instead of knife I could have bloody_knife[9] at the 0 index and the script should return 9, and 0, based on the query of knife.

The code:

import re

inventory = ["knife", "bottle[1]", "rope", "flashlight"]


def search_inventory(item):
    numbered_item = '.*' + item + '\[([0-9]*)\].*'
    print(numbered_item)                   #create regex statement
    regex_num_item = re.compile(numbered_item)
    print(regex_num_item)                  #compiled regex statement
    for x in item:
        match1 = regex_num_item.match(x)   #regex match....
        print(match1)                      #seems to be producing nothing.
        if match1:                         #since it produces nothing the code fails.
            num_item = match1.group()
            count = match1.group(1)
            print(count)
            index = inventory.index(num_item)
        else:                              #eventually this part will expand to include "item not in inventory"
            print("code is wrong")
        return count, index

num_of_item, item_index = search_inventory("knife")
print(num_of_item)
print(item_index)

The output:

.*knife\[([0-9]*)\].*
re.compile('.*knife.*\\[([0-9]*)\\].*')
None
code is wrong

One thing that I cannot seem to settle well with is when python takes the code in my numbered_item variable and uses it in the re.compile() function. why is it adding additional escapes when I already have the necessary [] escaped.

Has anyone run into something like this before?

9
  • 2
    What are you trying to do? Commented Jan 11, 2016 at 2:54
  • 1
    I am trying to search for an item in my inventory based on a simple name and return a item index and the amount of that item. Commented Jan 11, 2016 at 2:55
  • 1
    Can you reduce the code to exactly the problem you're having? From a rough guess, this could be one or more lines with only a regex and a (fixed) input string, together with expected output and actual output. Right now, we'd have to go through your whole function definition, which seems unnecessary for a regex problem. Commented Jan 11, 2016 at 3:12
  • 2
    "why is it adding additional escapes when I already have the necessary [] escaped": at a guess, you're only seeing additional escapes. To print an explicit backslash, Python prefixes such a backslash with a backslash ('\n' and '\\n' are two different beasts, and Python will therefore show them differently). So if you mean the double backslashes in '.*knife.*\\[([0-9]*)\\].*': those are still single backslashes. Commented Jan 11, 2016 at 3:14
  • 1
    I don't understand why you are even using regex here. You're not matching anything unusual a simple for x in inventory: if item in x would work. Even then you should be storing these items as keys in a dict with the value being the count. Commented Jan 11, 2016 at 3:47

1 Answer 1

1

Your issue is here:

 for x in item:

That is looking at "for every character in your item knife". So your regex was running on k, then n, and so on. Your regex won't want that of course. If you still wanted to "see it", add a print x:

for x in item:
        print x                            #add this line
        match1 = regex_num_item.match(x)   #regex match....
        print(match1)                      #seems to be producing nothing.

You'll see that it will print each letter of the item. That's what you're matching against in your match1 = regex_num_item.match(x) so obiously it won't work. You want to iterate over the inventory.

So you want:

 for x in inventory:    #meaning, for every item in inventory

Is the index important to you? Because you can change the inventory into a dictionary and you don't have to use regex:

inventory = {'knife':8, 'bottle':1, 'rope':1, 'flashlight':0, 'bloody_knife':1}

And then, if you wanted to find every item that has the word knife and how many you have of it:

for item in inventory:
    if "knife" in item:
        itemcount = inventory[item]           #in a dictionary, we get the 'value' of the key this way
        print "Item Name: " + item + "Count: " + str(itemcount)

Output:

Item Name: bloody_knife, Count: 1
Item Name: knife, Count: 8
Sign up to request clarification or add additional context in comments.

5 Comments

I feel rather stupid... although I still don't understand why it wouldn't match against item. Thank you, it seems the solution was in the place that I wasn't looking.
@Requiemsallure I've edited my answer. In your for x in item, that means "for every character in item" because item = knife right?
This is an excellent answer! The index will be important because it will allow me to mix it with a replace function. As an example if you have a bottle[4] or bottle:4 in the inventory and you want to gather water, I would use the replace definition to replace one bottle in the inventory using the inventory.remove(index) and then inventory.insert(index, "bottle[3]") and then add inventory.append("bottle(water)[1]") so whether i am searching for a item or whether the code is parsing for an item to replace. I wanted the definition to be flexible.
@Requiemsallure, you will still be able to do that in a dictionary. If you wanted to remove one bottle from the inventory, you would do inventory['bottle'] -= 1, which is short for inventory['bottle'] = inventory['bottle'] - 1, and obviously inventory['bottle'] refers to the value (quantity) of the key 'bottle'. Learn more about dictionaries man, they are powerful and very important.
Thank you, I will definitely look into this right away!

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.