|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
AT&T devCentral & BlackBerry(r) Webcast Series: BlackBerry and GPS -Build Location Awareness into your BlackBerry Applications, July 10th-1:00PM EST. Register Today!
|
|
#1
|
|||
|
|||
|
Parsing output
Is there any way for me to parse this output
Code:
chris_!~chris@bleh.net PRIVMSG #thisisatest :.say Hello to get the first word, and everything after it? For example, in the end "arg[0] == .say" and "arg[1] == 'Hello'" |
|
#2
|
||||
|
||||
|
Can you mark in bold the bits you want? Not sure exactly waht you want here although i can tell you it's not gonna be very hard.
Mark. |
|
#3
|
|||
|
|||
|
Hehe I was hoping it wouldn't be, but I'm a real noob with regex (that's what I tried).
Code:
chris_!~chris@bleh.net PRIVMSG #thisisatest :.say Hello I basicaly need the ".say" in arg[0] (or any other command it may be) and then the argument(s) in arg[1], arg[2], etc. In this case it would be "Hello" in arg[1]. Use another storage method if you know a better one ![]() Thanks in advance |
|
#4
|
||||
|
||||
|
Ok here it is, this may yet need to be changed depending on format of other commands but it works perfectly on this...
Code:
>>> parse = 'chris_!~chris@bleh.net PRIVMSG #thisisatest :.say Hello'
>>> index = parse.find(':')
>>> value = parse[index + 1:]
>>> command, message = value.split()
>>> command
'.say'
>>> message
'Hello'
>>>
This is because it works by finding the first ':' char and using it as an index; since i don't know enough about IRC commands format. Mark. |
|
#5
|
|||
|
|||
|
Ahhhh! I forgot a ':' at the beginning of the string... sorry about that? I hope it isn't hard to fix.
It all seems to look like this Code:
:chris_!~chris@bleh.net PRIVMSG #thisisatest :.say Hello |
|
#6
|
||||
|
||||
|
Naw not hard, just needs a different aproch
![]() Code:
>>> parse = ':chris_!~chris@bleh.net PRIVMSG #thisisatest :.say Hello'
>>> value = parse.split(':')
>>> value = value[-1]
>>> command, message = value.split()
>>> command
'.say'
>>> message
'Hello'
>>>
Note: This should work, and with any similar format (string:.command value). Mark. |
|
#7
|
|||
|
|||
|
Thanks alot Mark!
![]() |
|
#8
|
|||
|
|||
|
Another method to remember when searching through strings is that you can simplify a lot of problems if you reverse the string, then search, then reverse the result. This time, it's easy to do without; other times, it's invaluable.
|
|
#9
|
||||
|
||||
|
Perc, why not just use the rfind() string-method to search the string from the right?
Code:
>>> parse = ':chris_!~chris@bleh.net PRIVMSG #thisisatest :.say Hello'
>>> index = parse.rfind(':') + 1
>>> value = parse[index:]
>>> command, message = value.split()
>>> command
'.say'
>>> message
'Hello'
>>>
It seems so much simpler than doing... Code:
>>> parse = ':chris_!~chris@bleh.net PRIVMSG #thisisatest :.say Hello'
>>> value = parse[::-1]
>>> index = value.find(':')
>>> value = value[:index]
>>> value = value[::-1]
>>> command, message = value.split()
>>> command
'.say'
>>> message
'Hello'
>>>
...like you're recomending. So no need to manually reverse the string or results ![]() Mark. |
|
#10
|
|||
|
|||
|
Nice work netytan
![]() btw, where did you learn the 'PRIVMSG', 'JOIN', etc stuff for your bot? |
|
#11
|
|||
|
|||
|
Except that searching for the first ":" from the right isn't the right way to parse IRC messages
![]() First, a quick caveat. I know you're just playing around with this stuff to learn python, and that's cool and all. But, please realize that there are good IRC libraries out there that do this, so don't agonize over making a new one that does all this ![]() Okay, back to the topic at hand. You'll want to read the IRC RFC (1763, I think) to figure out the message structure. But the rightmost colon isn't necessarily what you are looking for. This is a valid message, for example: Code:
:chris_!~chris@bleh.net PRIVMSG #thisisatest :This is a valid message: foo And " foo" isn't the message you want, you want "This is a valid message: foo". |
|
#12
|
|||
|
|||
|
Hmm.. well that complicated things
![]() I'm not planning this to be a complicated bot so I want to try and write it without other libs. I just need the parser working right. By the way, wouldn't this work? Code:
>>> parse = ':chris_!~chris@bleh.net PRIVMSG #thisisatest :.say Hello:Hi'
>>> index = parse.rfind(':.') + 1
>>> value = parse[index:]
>>> value = value.split()
>>> value
['.say', 'Hello:Hi']
(netytan's code) Then, say the command was ".say" I could do something like this: Code:
>>> command = value[0] >>> text = ' '.join(value[1:]) >>> command '.say' >>> text 'Hello:Hi' Last edited by XxChris : January 31st, 2004 at 01:22 PM. |
|
#13
|
||||
|
||||
|
To be honest i know nothing about IRC commands; I was talking to Xlord on yahoo and he was explaining his functions so i just asked him some questions and wrote the IRC class for him
![]() Anyway just thought i'd mention that you can loose the whole join() method call etc by telling split() on the first space... Code:
>>> parse = ':chris_!~chris@bleh.net PRIVMSG #thisisatest :.say Hello:Hi baby'
>>> index = parse.rfind(':.') + 1
>>> value = parse[index:]
>>> command, message = value.split(' ', 1)
>>> comamnd
'.say'
>>> message
'Hello:Hi baby'
>>>
Nothing wrong with rewriting things - i do it all the time - its a good way to learn. Plus, sometimes you end up with a better version than whats already out there. I mean.. would you have the IRC libs you have today if everyone just stuck with the first one? Mark. |
|
#14
|
|||
|
|||
|
what is the '.' at the beginning of the message for?
|
|
#15
|
||||
|