473,396 Members | 1,767 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

string split without consumption

this didn't work elegantly as expected:
>>ss
'owi\nweoifj\nfheu\n'
>>re.split(r'\A',ss)
['owi\nweoifj\nfheu\n']
>>re.split(r'\Z',ss)
['owi\nweoifj\nfheu\n']
>>re.split(r'$',ss)
['owi\nweoifj\nfheu\n']
>>re.split(r'(?s)$',ss)
['owi\nweoifj\nfheu\n']
>>re.split(r'(?m)(?s)$',ss)
['owi\nweoifj\nfheu\n']
>>re.split(r'(?m)$',ss)
['owi\nweoifj\nfheu\n']
>>re.split(r'(?m)\Z',ss)
['owi\nweoifj\nfheu\n']
>>re.split(r'(?m)\A',ss)
['owi\nweoifj\nfheu\n']
>>re.split(r'(?s)\A',ss)
['owi\nweoifj\nfheu\n']
>>re.split(r'(?s)(?m)\A',ss)
['owi\nweoifj\nfheu\n']
>>>

how to do?
Robert
Feb 2 '08 #1
5 1330
this didn't work elegantly as expected:
>
>>ss
'owi\nweoifj\nfheu\n'
>>re.split(r'(?m)$',ss)
['owi\nweoifj\nfheu\n']
Do you have a need to use a regexp?
>>ss.splitlines(True)
['owi\n', 'weoifj\n', 'fheu\n']

-tkc


Feb 2 '08 #2
Tim Chase wrote:
>this didn't work elegantly as expected:
> >>ss
'owi\nweoifj\nfheu\n'
> >>re.split(r'(?m)$',ss)
['owi\nweoifj\nfheu\n']

Do you have a need to use a regexp?
I'd like the general case - split without consumption.
>
>>>ss.splitlines(True)
['owi\n', 'weoifj\n', 'fheu\n']
thanks. Yet this does not work "naturally" consistent in my line
processing algorithm - the further buffering. Compare e.g.
ss.split('\n') ..
>>'owi\nweoifj\nfheu\n'.split('\n')
['owi', 'weoifj', 'fheu', '']
>>'owi\nweoifj\nfheu\nxx'.split('\n')
['owi', 'weoifj', 'fheu', 'xx']

is consistent in that regard: there is always a last empty or half
line, which can be fed readily as start to the further input
buffering.
With the .splitlines(True/False) results you need to fiddle, test
the last result's last char... Or you fail altogether with False.
So I'd call this a "wrong" implementation.
Robert
Feb 2 '08 #3
robert wrote:
thanks. Yet this does not work "naturally" consistent in my line
processing algorithm - the further buffering. Compare e.g.
ss.split('\n') Â*..
>'owi\nweoifj\nfheu\n'.split('\n')
['owi', 'weoifj', 'fheu', '']
>'owi\nweoifj\nfheu\nxx'.split('\n')
['owi', 'weoifj', 'fheu', 'xx']

Maybe this works for you?
>>re.split(r'(\n)', ss)
['owi', '\n', 'weoifj', '\n', 'fheu', '\n', '']
Jeffrey
Feb 2 '08 #4
Jeffrey Froman wrote:
robert wrote:
>thanks. Yet this does not work "naturally" consistent in my line
processing algorithm - the further buffering. Compare e.g.
ss.split('\n') ..
>>>>'owi\nweoifj\nfheu\n'.split('\n')
['owi', 'weoifj', 'fheu', '']
>>>>'owi\nweoifj\nfheu\nxx'.split('\n')
['owi', 'weoifj', 'fheu', 'xx']


Maybe this works for you?
>>>re.split(r'(\n)', ss)
['owi', '\n', 'weoifj', '\n', 'fheu', '\n', '']
Thanks, thats it
Robert
Feb 2 '08 #5
robert wrote:
[...]
but its also wrong regarding partial last lines.

re.split obviously doesn't understand \A \Z ^ $ and also \b etc.
empty matches.
[...]
Or perhaps you don't understand re?

It's a tricky thing to start playing with. Look up re.MULTILINE ans
re.DOTALL.

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/

Feb 2 '08 #6

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

Similar topics

4
by: Simon Schaap | last post by:
Hello, I have encountered a strange problem and I hope you can help me to understand it. What I want to do is to pass an array of chars to a function that will split it up (on every location where...
23
by: Rogers | last post by:
I want to compare strings of numbers that have a circular boundary condition. This means that the string is arranged in a loop without an end-of-string. The comparaison of two strings now...
16
by: Charles Law | last post by:
I have a string similar to the following: " MyString 40 "Hello world" all " It contains white space that may be spaces or tabs, or a combination, and I want to produce an array...
6
by: Diffident | last post by:
Dear Bruce, Can you please explain me how compiler would be able to optimize "+" for the below code? The reason why I did not want to use string concatenation was that I wanted it to be a...
4
by: VMI | last post by:
How can I split a string that looks like this: John, Doe, 37282, box 2, 10001, "My description, very important", X, Home If I use String.Split(), it'll split the string that's between the...
7
by: Matt | last post by:
I am attempting to reformat a string, inserting newlines before certain phrases. For example, in formatting SQL, I want to start a new line at each JOIN condition. Noting that strings are...
2
by: eddiefisher41 | last post by:
Hey Guys. If I have a string say "abcdefg" how do i split the string without delimiters. What i need is a list containing each of the chars: e.g. The string.split() method wont work as it...
3
by: Dave | last post by:
I'm calling string.Split() producing output string. I need direct access to its enumerator, but would greatly prefer an enumerator strings and not object types (as my parsing is unsafe casting...
37
by: xyz | last post by:
I have a string 16:23:18.659343 131.188.37.230.22 131.188.37.59.1398 tcp 168 for example lets say for the above string 16:23:18.659343 -- time 131.188.37.230 -- srcaddress 22 ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...

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.