473,508 Members | 2,441 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

text processing

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
Sep 25 '08 #1
3 1109
On Thu, 25 Sep 2008 15:51:28 +0100, ji*********@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))

Ciao,
Marc 'BlackJack' Rintsch
Sep 25 '08 #2
On Sep 25, 6:34*pm, Marc 'BlackJack' Rintsch <bj_...@gmx.netwrote:
On Thu, 25 Sep 2008 15:51:28 +0100, jitensha...@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')]
Sep 25 '08 #3
On Sep 25, 9:51*am, "jitensha...@gmail.com" <jitensha...@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
Looks like each item is:
- a list of 1 or more integers, in a comma-delimited list
- a slash
- a word composed of alpha characters

And the whole thing is a list of items in a comma-delimited list

Now to implement that in pyparsing:
>>data = "12560/ABC,12567/BC,123,567,890/JK"
from pyparsing import Suppress, delimitedList, Word, alphas, nums, Group
SLASH = Suppress('/')
dataitem = delimitedList(Word(nums)) + SLASH + Word(alphas)
dataformat = delimitedList(Group(dataitem))
map(tuple, dataformat.parseString(data))
[('12560', 'ABC'), ('12567', 'BC'), ('123', '567', '890', 'JK')]

Wah-lah! (as one of my wife's 1st graders announced in one of his
school papers)

-- Paul
Sep 26 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
3116
by: Martin | last post by:
Hallo, can you help me writing a generic xslt transformation (useable with xsql from oracle)? The problem is how to get the escaping characters .... === INPUT-File in.xml <?xml version =...
8
7442
by: Imran | last post by:
hello all, I have to parse a text file and get some value in that. text file content is as follows. ####TEXT FILE CONTENT STARTS HERE ##### /start first 0x1234 AC /end
1
1674
by: daldridge | last post by:
I have a unique-elements/sorting question (who doesn't?), but haven't yet been able to get appropriate template/select/for-each processing working. I don't fully grok the Muenchian technique yet...
1
1260
by: Shyran | last post by:
we have a java framework, where we feed a request xml. this request xml is forwarded through the framework, tomcat and axis, for the backend processing, and the processing results are again...
6
24132
by: ivan.perak | last post by:
Hello, im a beginner in VB.NET... The thing i would like to do is as it follows.... I have a text file (list of names, every name to the next line) which is about 350000 lines long. I would...
16
11036
by: Neil | last post by:
I posted a few days ago that it seems to me that the Access 2007 rich text feature does not support: a) full text justification; b) programmatic manipulation. I was hoping that someone might...
0
4382
by: JosAH | last post by:
Greetings, Introduction At the end of the last Compiler article part I stated that I wanted to write about text processing. I had no idea what exactly to talk about; until my wife commanded...
0
4428
by: JosAH | last post by:
Greetings, Introduction Last week I was a bit too busy to cook up this part of the article series; sorry for that. This article part wraps up the Text Processing article series. The ...
1
3415
by: Xah Lee | last post by:
Text Processing with Emacs Lisp Xah Lee, 2007-10-29 This page gives a outline of how to use emacs lisp to do text processing, using a specific real-world problem as example. If you don't know...
3
1917
by: Rinaldo | last post by:
Hi, I have a label on my dialogbox who has to change text while running. This is what I do: lblBackup.Text = "Bezig met de backup naar " + F1.FTPserver; but the text does'nt appear, only if...
0
7223
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7114
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
7034
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7488
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5623
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3191
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1544
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
762
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.