text processing
MRAB
google at mrabarnett.plus.com
Thu Sep 25 18:01:15 EDT 2008
On Sep 25, 6:34 pm, Marc 'BlackJack' Rintsch <bj_... at gmx.net> wrote:
> On Thu, 25 Sep 2008 15:51:28 +0100, jitensha... at gmail.com wrote:
> > I have string like follow
> > 12560/ABC,12567/BC,123,567,890/JK
>
> > I want above string to group like as follow (12560,ABC)
> > (12567,BC)
> > (123,567,890,JK)
>
> > i try regular expression i am able to get first two not the third one.
> > can regular expression given data in different groups
>
> Without regular expressions:
>
> def group(string):
> result = list()
> for item in string.split(','):
> if '/' in item:
> result.extend(item.split('/'))
> yield tuple(result)
> result = list()
> else:
> result.append(item)
>
> def main():
> string = '12560/ABC,12567/BC,123,567,890/JK'
> print list(group(string))
>
How about:
>>> string = "12560/ABC,12567/BC,123,567,890/JK"
>>> r = re.findall(r"(\d+(?:,\d+)*/\w+)", string)
>>> r
['12560/ABC', '12567/BC', '123,567,890/JK']
>>> [tuple(x.replace(",", "/").split("/")) for x in r]
[('12560', 'ABC'), ('12567', 'BC'), ('123', '567', '890', 'JK')]
More information about the Python-list
mailing list