Search code examples
pythonlistreplacereducestring-matching

how to find the matching pattern for an input list and then replace the found pattern with the proper pattern conversion using python


note that the final two numbers of this pattern for example FBXASC048 are ment to be ascii code for numbers (0-9)

input example list ['FBXASC048009Car', 'FBXASC053002Toy', 'FBXASC050004Human'] result example ['1009Car', '5002Toy', '2004Human']

what is the proper way to searches for any of these pattern in an input list

num_ascii = ['FBXASC048', 'FBXASC049', 'FBXASC050', 'FBXASC051', 'FBXASC052', 'FBXASC053', 'FBXASC054', 'FBXASC055', 'FBXASC056', 'FBXASC057']

and then replaces the pattern found with one of the items in the conv list but not randomally because each element in the pattern list equals only one element in the conv_list

conv_list = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']

this is the solution in mind: it has two part

1st part--> is to find for ascii pattern[48, 49, 50, 51, 52, 53, 54, 55, 56,57] and then replace those with the proper decimal matching (0-9) so we will get new input list will be called input_modi_list that has ascii replaced with decimal 2nd part-->another process to use fixed pattern to replace using replace function which is this 'FBXASC0' new_list3

for x in input_modi_list:
    y = x.replace('FBXASC0', '')
    new_list3.append(new_string)  

so new_list3 will have the combined result of the two parts mentioned above.

i don't know if there would be a simplar solution or a better one maybe using regex also note i don't have any idea on how to replace ascii with decimal for a list of items


Solution

  • I think this should do the trick:

    import re
    
    input_list = ['FBXASC048009Car', 'FBXASC053002Toy', 'FBXASC050004Human']
    
    pattern = re.compile('FBXASC(\d{3,3})')
    def decode(match):
        return chr(int(match.group(1)))
    result = [re.sub(pattern, decode, item) for item in input_list]
    
    print(result)
    

    Now, there is some explanation due:

    1- the pattern object is a regular expression that will match any part of a string that starts with 'FBXASC' and ends with 3 digits (0-9). (the \d means digit, and {3,3} means that it should occur at least 3, and at most 3 times, i.e. exactly 3 times). Also, the parenthesis around \d{3,3} means that the three digits matched will be stored for later use (explained in the next part).

    2- The decode function receives a match object, uses .group(1) to extract the first matched group (which in our case are the three digits matched by \d{3,3}), then uses the int function to parse the string into an integer (for example, convert '048' to 48), and finally uses the chr function to find which character has that ASCII-code. (for example chr(48) will return '0', and chr(65) will return 'A')

    3- The final part applies the re.sub function to all elements of list which will replace each occurrence of the pattern you described (FBXASC048[3-digits]) with it's corresponding ASCII character.

    You can see that this solution is not limited only to your specific examples. Any number can be used as long as it has a corresponding ASCII character recognized by the chr function.

    But, if you do want to limit it just to the 48-57 range, you can simply modify the decode function:

    def decode(match):
        ascii_code = int(match.group(1))
        if ascii_code >= 48 and ascii_code <= 57:
            return chr(ascii_code)
        else:
            return match.group(0) # returns the entire string - no modification