2016-04-05 5 views
1

Ich bin auf der Suche nach dem Muster, das mir hilft, eine Zeichenfolge zu schneiden. Der String ist so etwas wie diese:Spezifisches Python-Muster für die Zeichenfolge, die beim Slicen helfen kann

text = '1. first slice 2. second slice 3. slice number 3 4. the next one 
5 that will not work but belong to no four 5. and this should be 5 and 
so one...' 

ich diese erhalten mag:

  1. erste Scheibe
  2. Sekunden Scheibe
  3. Slice-Nummer 3
  4. die nächsten 5, die nicht funktionieren aber gehören zu keiner vier
  5. und das sollte 5 und so weiter sein ...

Ich hoffe, Sie haben die Idee.

Was bis ich jetzt untersucht haben, ist, dass ich diese verwenden:

import re 

parts = re.findall("\d\\. \D+", text) 

Das funktioniert gut, bis es einzelne Zahl begegnen. Ich weiß, dass \ D Ausdruck nicht Ziffer ist, und ich versuchte, zu verwenden:

parts = re.findall("\d\\. .+,text) 

oder

parts = re.findall("(\d\\.).*,text) 

und viele andere, aber ich kann nicht die richtigen finden.

Ich werde für Ihre Hilfe dankbar sein.

+0

Vielleicht helfen könnte? http://stackoverflow.com/questions/2260280/what-is-the-regex-for-a-number-followed-by-a-period –

+0

@MikkelBueTellus - Ich denke nicht, dass das sehr hilft, wie das schon ist hier benutzt werden. – TigerhawkT3

+0

Es wäre so schön, wenn 'r' \ d \. . *? «» Hat funktioniert. Alles andere sieht nur aus wie ein Workaround. – TigerhawkT3

Antwort

0

Sie könnten eine negative Vorschau verwenden:

parts = re.findall(r"\d\. (?:\D+|\d(?!\.))*", text) 

gefolgt Dies entspricht einer Ziffer und Punkt, durch alles an allen vorgesehen, dass alle Ziffern nicht direkt gefolgt von einem Punkt sind.

Demo:

>>> import re 
>>> text = '1. first slice 2. second slice 3. slice number 3 4. the next one 5 that will not work but belong to no four 5. and this should be 5 and so one...' 
>>> re.findall(r"\d\. (?:\D+|\d(?!\.))*", text) 
['1. first slice ', '2. second slice ', '3. slice number 3 ', '4. the next one 5 that will not work but belong to no four ', '5. and this should be 5 and so one...'] 

Online-Demo auf https://regex101.com/r/kF9jT1/1; Um das re.findall() Verhalten zu simulieren, fügte ich ein extra (..) und das g Flag hinzu.

+0

Das funktioniert, großartig! Und danke für schönes Online-Tool. – Muddler

0

Nur basierend auf einem lookahead aufgeteilt.

x="""1. first slice 2. second slice 3. slice number 3 4. the next one 
5 that will not work but belong to no four 5. and this should be 5 and 
so one...""" 
print re.split(r"\s(?=\d+\.\s)",x) 

Ausgang: ['1. first slice', '2. second slice', '3. slice number 3', '4. the next one\n 5 that will not work but belong to no four', '5. and this should be 5 and\n so one...']

0

Dies sollte funktionieren

(#First group to be captured 
    \d+\..*? #Match digit(s) followed by decimal and make it non-greedy 
) 
(?= #Lookahed 
    \d+\. #Check if what follows is digit(s) followed by decimal 
    | #or 
    $ #End of string 
) 

Regex Demo

Regex Pannen

(\d+\..*?)(?=\d+\.|$) 

Python-Code

import re 
text = '1. first slice 2. second slice 3. slice number 3 4. the next one 5 that will not work but belong to no four 5. and this should be 5 and so one...' 
parts = re.findall(r"(\d+\..*?)(?=\d+\.|$)", text) 
print(parts) 

Ideone Demo