rustysys-dev
rustysys-dev

Reputation: 863

Python Regex Failure....?

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?

Upvotes: 0

Views: 60

Answers (1)

Alaa Ali
Alaa Ali

Reputation: 926

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

Upvotes: 1

Related Questions