472,958 Members | 2,568 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 software developers and data experts.

splitting a words of a line

Hi ,
I am trying to splitt a Line whihc is below of format ,

AzAccept PLYSSTM01 [23/Sep/2005:16:14:28 -0500] "162.44.245.32 CN=dddd
cojack (890),OU=1,OU=Customers,OU=ISM-Users,OU=kkk
Secure,DC=customer,DC=rxcorp,DC=com" "plysmhc03zp GET /mci/performance/
SelectProducts.aspx?
p=0&V=C&a=29&menu=adhoc" [d4b62ca2-09a0-4334622b-0e1c-03c42ba5] [0]

Here all the string whihc i want to split is
---------------------------------
AzAccept
PLYSSTM01
[23/Sep/2005:16:14:28 -0500]
162.44.245.32
CN=dddd cojack (890),OU=1,OU=Customers,OU=ISM-Users,OU=kkk
Secure,DC=customer,DC=rxcorp,DC=com"
GET
/mci/performance/SelectProducts.aspx?p=0&V=C&a=29&menu=adhoc
d4b62ca2-09a0-4334622b-0e1c-03c42ba5
0
--------------------------------

i am trying to use re.split() method to split them , But unable to get
the exact result .

Any help on this is highly appriciated .

Thanks
Sumit
Dec 6 '07 #1
2 1306
On Dec 7, 2:21 am, Sumit <sumit.na...@gmail.comwrote:
Hi ,
I am trying to splitt a Line whihc is below of format ,

AzAccept PLYSSTM01 [23/Sep/2005:16:14:28 -0500] "162.44.245.32 CN=dddd
cojack (890),OU=1,OU=Customers,OU=ISM-Users,OU=kkk
Secure,DC=customer,DC=rxcorp,DC=com" "plysmhc03zp GET /mci/performance/
SelectProducts.aspx?
p=0&V=C&a=29&menu=adhoc" [d4b62ca2-09a0-4334622b-0e1c-03c42ba5] [0]
Because lines are mangled in transmission, it is rather difficult to
guess exactly what you have in your input and what your expected
results are.

Also you don't show exactly what you have tried.

At the end is a small script that contains my guess as to your input
and expected results, shows an example of what the re.VERBOSE flag is
intended for, and how you might debug your results.

So that you don't get your homework done 100% for free, I haven't
corrected the last mistake I made.

As usual, re may not be the best way of doing this exercise. Your
*single* piece of evidence may not be enough. It appears to be a
horrid conglomeration of instances of different things, each with its
own grammar. You may find that something like PyParsing would be more
legible and more robust.
>
Here all the string whihc i want to split is
---------------------------------
AzAccept
PLYSSTM01
[23/Sep/2005:16:14:28 -0500]
162.44.245.32
CN=dddd cojack (890),OU=1,OU=Customers,OU=ISM-Users,OU=kkk
Secure,DC=customer,DC=rxcorp,DC=com"
GET
/mci/performance/SelectProducts.aspx?p=0&V=C&a=29&menu=adhoc
d4b62ca2-09a0-4334622b-0e1c-03c42ba5
0
--------------------------------

i am trying to use re.split() method to split them , But unable to get
the exact result .
C:\junk>type sumit.py
import re

textin = \
"""AzAccept PLYSSTM01 [23/Sep/2005:16:14:28 -0500] "162.44.245.32
CN=dddd """ \
"""cojack (890),OU=1,OU=Customers,OU=ISM-Users,OU=kkk """ \
"""Secure,DC=customer,DC=rxcorp,DC=com" "plysmhc03zp GET /mci/
performance/""" \
"""SelectProducts.aspx?""" \
"""p=0&V=C&a=29&menu=adhoc" [d4b62ca2-09a0-4334622b-0e1c-03c42ba5]
[0]"""

expected = [
"AzAccept",
"PLYSSTM01",
"23/Sep/2005:16:14:28 -0500",
"162.44.245.32",
"CN=dddd cojack (890),OU=1,OU=Customers,OU=ISM-Users,OU=kkk
Secure,DC=custom
er,DC=rxcorp,DC=com",
"plysmhc03zp",
"GET",
"/mci/performance/SelectProducts.aspx?p=0&V=C&a=29&menu=adhoc",
"d4b62ca2-09a0-4334622b-0e1c-03c42ba5",
"0",
]

pattern = r"""
(\S+) # AzAccept
\s+
(\S+) # PLYSSTM01
\s+\[
([^]]+) # 23/Sep/2005:16:14:28 -0500
]\s+"
(\S+) # 162.44.245.32
\s+
([^"]+) # CN=dddd cojack (890),OU=1, etc etc,DC=rxcorp,DC=com
"\s+"
(\S+) # plysmhc03zp
\s+
(\S+) # GET
\s+
(\S+) # /mci/performance/ ... menu=adhoc
\s+\[
([^]]+) # d4b62ca2-09a0-4334622b-0e1c-03c42ba5
]\s+\[
([^]]+) # 0
]$
"""

mobj = re.match(pattern, textin, re.VERBOSE)
if not mobj:
print "Bzzzt!"
else:
result = mobj.groups()
print "len check", len(result) == len(expected), len(result),
len(expected)
for a, b in zip(result, expected):
print a == b, repr(a), repr(b)

C:\junk>python sumit.py
len check True 10 10
True 'AzAccept' 'AzAccept'
True 'PLYSSTM01' 'PLYSSTM01'
True '23/Sep/2005:16:14:28 -0500' '23/Sep/2005:16:14:28 -0500'
True '162.44.245.32' '162.44.245.32'
True 'CN=dddd cojack (890),OU=1,OU=Customers,OU=ISM-Users,OU=kkk
Secure,DC=custo
mer,DC=rxcorp,DC=com' 'CN=dddd cojack (890),OU=1,OU=Customers,OU=ISM-
Users,OU=kk
k Secure,DC=customer,DC=rxcorp,DC=com'
True 'plysmhc03zp' 'plysmhc03zp'
True 'GET' 'GET'
False '/mci/performance/SelectProducts.aspx?p=0&V=C&a=29&menu=adhoc"'
'/mci/perf
ormance/SelectProducts.aspx?p=0&V=C&a=29&menu=adhoc'
True 'd4b62ca2-09a0-4334622b-0e1c-03c42ba5'
'd4b62ca2-09a0-4334622b-0e1c-03c42ba
5'
True '0' '0'

C:\junk>
Dec 6 '07 #2
On Dec 6, 9:21 am, Sumit <sumit.na...@gmail.comwrote:
Hi ,
I am trying to splitt a Line whihc is below of format ,

AzAccept PLYSSTM01 [23/Sep/2005:16:14:28 -0500] "162.44.245.32 CN=dddd
cojack (890),OU=1,OU=Customers,OU=ISM-Users,OU=kkk
Secure,DC=customer,DC=rxcorp,DC=com" "plysmhc03zp GET /mci/performance/
SelectProducts.aspx?
p=0&V=C&a=29&menu=adhoc" [d4b62ca2-09a0-4334622b-0e1c-03c42ba5] [0]
As John Machin mentioned, pyparsing may be helpful to you. Here is a
simple version:

data = """AzAccept PLYSSTM01 [23/Sep/2005:16:14:28 -0500]
"162.44.245.32 CN=dddd cojack (890),OU=1,OU=Customers,OU=ISM-
Users,OU=kkk Secure,DC=customer,DC=rxcorp,DC=com" "plysmhc03zp GET /
mci/performance/SelectProducts.aspx?
p=0&V=C&a=29&menu=adhoc" [d4b62ca2-09a0-4334622b-0e1c-03c42ba5] [0]"""

# Version 1 - simple
from pyparsing import *
LBRACK,RBRACK,COMMA = map(Suppress,"[],")
num = Word(nums)
date = Combine(num+"/"+Word(alphas)+"/"+num+":"+num+":"+num+":"+num) +
\
oneOf("+ -") + num
date.setParseAction(keepOriginalText)
uuid = delimitedList(Word(hexnums),"-",combine=True)
logString = Word(alphas,alphanums) + Word(alphas,alphanums) + \
LBRACK + date + RBRACK + quotedString + quotedString + \
LBRACK + uuid + RBRACK + LBRACK + Word(nums) + RBRACK

print logString.parseString(data)

Prints out:
['AzAccept', 'PLYSSTM01', '23/Sep/2005:16:14:28 -0500',
'"162.44.245.32 CN=dddd cojack (890),OU=1,OU=Customers,OU=ISM-
Users,OU=kkk Secure,DC=customer,DC=rxcorp,DC=com"', '"plysmhc03zp GET /
mci/performance/SelectProducts.aspx?p=0&V=C&a=29&menu=adhoc"',
'd4b62ca2-09a0-4334622b-0e1c-03c42ba5', '0']
And here is a slightly fancier version, which parses the quoted
strings (uses the pprint pretty-printing module to show structure of
the parsed results):

# Version 2 - fancy
from pyparsing import *
LBRACK,RBRACK,COMMA = map(Suppress,"[],")
num = Word(nums)
date = Combine(num+"/"+Word(alphas)+"/"+num+":"+num+":"+num+":"+num) +
\
oneOf("+ -") + num
date.setParseAction(keepOriginalText)
uuid = delimitedList(Word(hexnums),"-",combine=True)

ipAddr = delimitedList(Word(nums),".",combine=True)
keyExpr=Word(alphas.upper())
valExpr=CharsNotIn(',')
qs1Expr = ipAddr + Group(delimitedList(Combine(keyExpr + '=' +
valExpr)))
def parseQS1(t):
return qs1Expr.parseString(t[0])
def parseQS2(t):
return t[0].split()

qs1 = quotedString.copy().setParseAction(removeQuotes, parseQS1)
qs2 = quotedString.copy().setParseAction(removeQuotes, parseQS2)

logString = Word(alphas,alphanums) + Word(alphas,alphanums) + \
LBRACK + date + RBRACK + qs1 + qs2 + \
LBRACK + uuid + RBRACK + LBRACK + Word(nums) + RBRACK

from pprint import pprint
pprint(logString.parseString(data).asList())

Prints:
['AzAccept',
'PLYSSTM01',
'23/Sep/2005:16:14:28 -0500',
'162.44.245.32',
['CN=dddd cojack (890)',
'OU=1',
'OU=Customers',
'OU=ISM-Users',
'OU=kkk Secure',
'DC=customer',
'DC=rxcorp',
'DC=com'],
'plysmhc03zp',
'GET',
'/mci/performance/SelectProducts.aspx?p=0&V=C&a=29&menu=adhoc',
'd4b62ca2-09a0-4334622b-0e1c-03c42ba5',
'0']

Find more about pyparsing at http://pyparsing.wikispaces.com.

-- Paul
Dec 7 '07 #3

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

Similar topics

2
by: Joe | last post by:
hi, i have a search form that will only search the whole string when searching a query. i would like to have the search string split into separate words so that each word could be used as part...
20
by: Opettaja | last post by:
I am new to c# and I am currently trying to make a program to retrieve Battlefield 2 game stats from the gamespy servers. I have got it so I can retrieve the data but I do not know how to cut up...
15
by: Daren | last post by:
Hi, I need to be able to split large string variables into an array of lines, each line can be no longer than 70 chars. The string variables are text, so I would additionally like the lines...
7
by: Anat | last post by:
Hi, What regex do I need to split a string, using javascript's split method, into words-array? Splitting accroding to whitespaces only is not enough, I need to split according to whitespace,...
2
by: Anat | last post by:
Hi, I need a little help on performing string manipulation: I want to take a given string, and make certain words hyperlinks. For example: "Hello world, this is a wonderful day!" I'd like the...
12
by: Simon | last post by:
Well, the title's pretty descriptive; how would I be able to take a line of input like this: getline(cin,mostrecentline); And split into an (flexible) array of strings. For example: "do this...
17
by: Qiangning Hong | last post by:
I've got some strings to split. They are main words, but some words are inside a pair of brackets and should be considered as one unit. I prefer to use re.split, but haven't written a working one...
9
by: conspireagainst | last post by:
I'm having quite a time with this particular problem: I have users that enter tag words as form input, let's say for a photo or a topic of discussion. They are allowed to delimit tags with spaces...
2
by: shadow_ | last post by:
Hi i m new at C and trying to write a parser and a string class. Basicly program will read data from file and splits it into lines then lines to words. i used strtok function for splitting data to...
4
by: techusky | last post by:
I am making a website for a newspaper, and I am having difficulty figuring out how to take a string (the body of an article) and break it up into three new strings so that I can display them in the...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

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.