2016-04-05 21 views
1

Bir dizeyi kesmeme yardımcı olan deseni arıyorum.Dilime yardımcı olabilecek dizeye özgü Python kalıbı

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...' 

Bunu almak istiyorum:

  • ikinci dilim
  • dilim sayısı 3
  • sonraki çalışmayacak 5 o

    1. ilk dilim dize böyle bir şeydir ama dört
    2. 'a aittir ve bu 5 olmalıdır ve böyle olmalıdır ...

    Umarım fikrin vardır. İyi işler

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

    o tek bir numara karşılaşana kadar: Ben şu ana kadar inceledik Ne

    Bunu kullanabilirim olmasıdır. Ben D \ ifadesi olmayan haneli olduğunu biliyorum ve kullanmaya çalıştı:

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

    veya

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

    ve diğerleri

    ama doğru olanı bulmak olamaz.

    Yardımlarınız için minnettar olacağım. Bu her de şey tarafından takip edilen bir rakam ve nokta, kibrit

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

    herhangi rakam doğrudan bir nokta tarafından takip edilmez şartıyla:

  • +0

    Belki bu yardımcı olabilir? http://stackoverflow.com/questions/2260280/what-is-the-regex-for-a-number-followed-by-a-period –

    +0

    @MikkelBueTellus - Bence bu zaten çok yardımcı oluyor. burada kullanılıyor. – TigerhawkT3

    +0

    Eğer 'r' \ d \ çok güzel olurdu. . *? '' çalıştı. Her şey sadece bir geçici çözüm gibi görünüyor. – TigerhawkT3

    cevap

    0

    Sen bir negatif ileri yönlü kullanabilirsiniz.

    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...'] 
    

    https://regex101.com/r/kF9jT1/1 Online demo; re.findall() davranışını taklit etmek için ek bir (..) ve g bayrağını ekledim.

    +0

    Bu işe yarıyor, harika! Ve güzel çevrimiçi araç için teşekkürler. – Muddler

    0

    Yalnızca lookahead'a göre bölün.

    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) 
    

    Çıktı: ['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

    Bu çalışması gerekir

    (#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 Dağılımı

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

    Python kodu

    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