[Tutor] Clarification on using regular expression

Alan Gauld alan.gauld at btinternet.com
Fri Aug 30 11:47:27 EDT 2019


On 30/08/2019 11:24, Arulanand Nagarajan (RBEI/EEV51-EC) via Tutor wrote:
> I am currently working on a python script which uses regular expressions for processing some data.
> I have some string as below(each in new line):
> aa1 !=5
> bb1 >=1
> cc1 ==1
> dd2 <= 2
> e3 <2
> >From this string, I want to get a list of all the labels that are before the comparison operator and another list that contains the values after the comparison operator.
> That is
> list1 = ['aa1','bb1','cc1','dd2','e3']
> list2 = ['5','1','1','2','2']
> How will I be able to do that?

Without regular expressions hopefully.

Try:

>>> ops = " !=<>+-/|^~"   # modify to contain all your symbols
>>> strings = ["aa1 !=5", "bb1 >=1","cc1 ==1","dd2 <= 2","e3 <2"]

>>> def getValues(string): return (v.lstrip(ops) for v in string.split())

>>> results = [getValues(s) for s in strings]

You could use a regex here but I think the non regex version is easier.

HTH

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos



More information about the Tutor mailing list