LearningNewbie 0 Posted October 10, 2016 Report Share Posted October 10, 2016 Hey guys! I am new to learning regex in python and I'm wondering how do I use regex in python to store the integers(positive and negative) i want into a list! For e.g. This is the data in a list. [u'\x1b[0m[\x1b[1m\x1b[0m\xbb\x1b[0m\x1b[36m]\x1b[0m (A=-5,B=5)', u'\x1b[0m[\x1b[1m\x1b[0m\xbb\x1b[0m\x1b[36m]\x1b[0m (A=5,Y=5)', u'\x1b[0m[\x1b[1m\x1b[10m\xbb\x1b[0m\x1b[36m]\x1b[0m : '] How do I extract the values of A and B and store them in a variable I want using regex? Thank you and appreciate it Quote Link to post Share on other sites
Code Docta (Nick C.) 638 Posted October 11, 2016 Report Share Posted October 11, 2016 Hi, Try without regext. Hope that helps, Nick Quote Link to post Share on other sites
LearningNewbie 0 Posted October 11, 2016 Author Report Share Posted October 11, 2016 Hi! I want to store the numbers in a variable be it positive or negative! I've tried this:For exampleThis is the data in a list.data =[u'\x1b[0m[\x1b[1m\x1b[0m\xbb\x1b[0m\x1b[36m]\x1b[0m (A=-5,B=5)',u'\x1b[0m[\x1b[1m\x1b[0m\xbb\x1b[0m\x1b[36m]\x1b[0m (A=5,Y=5)',u'\x1b[0m[\x1b[1m\x1b[10m\xbb\x1b[0m\x1b[36m]\x1b[0m : '] How do I extract the integer values of A and B (negative and positive) and store them in a variable so that I can work with the numbers?I tried smth like this but the list is empty ..for line in data[0]:pattern = re.compile("([A-Z]=(-?\d+?),[A-Z]=(-?\d+?))")store = pattern.findall(line)print store Thank you and appreciate it Quote Link to post Share on other sites
Code Docta (Nick C.) 638 Posted October 11, 2016 Report Share Posted October 11, 2016 In ubot? Have you watched any re vids? Quote Link to post Share on other sites
Code Docta (Nick C.) 638 Posted October 11, 2016 Report Share Posted October 11, 2016 Here you go... import restore = []for i in data: vals = re.findall(r'([A-Z]=(-?\d+?),[A-Z]=(-?\d+?))', i) for n in vals: print n[1] + ',' + n[2] store.append(n[1] + ',' + n[2])print store Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.