Visualizzazione dei risultati da 1 a 8 su 8
  1. #1

    [VB2008] Codice che "si blocca"

    Ho questo semplice codice:
    codice:
    Private Shared ReadOnly SentenceRegex As New Regex("(?<Word>\W?\w+\W?)+?(\.|\!|\?)", RegexOptions.Multiline Or RegexOptions.Compiled)
    
    '...
    
    Dim Content As String = IO.File.ReadAllText(FileName)
    '...
    Dim Matches As MatchCollection = SentenceRegex.Matches(Content)
    For Each M As Match In Matches
        Debug.WriteLine(M.Value)
    Next
    Content vale:
    codice:
    INTRODUCTION.
    
    When on board HMS 'Beagle,' as naturalist, I was much struck with
    certain facts in the distribution of the inhabitants of South America,
    and in the geological relations of the present to the past inhabitants
    of that continent. These facts seemed to me to throw some light on the
    origin of species--that mystery of mysteries, as it has been called by
    one of our greatest philosophers. On my return home, it occurred to
    me, in 1837, that something might perhaps be made out on this question
    by patiently accumulating and reflecting on all sorts of facts which
    could possibly have any bearing on it. After five years' work I
    allowed myself to speculate on the subject, and drew up some short
    notes; these I enlarged in 1844 into a sketch of the conclusions,
    which then seemed to me probable: from that period to the present day
    I have steadily pursued the same object. I hope that I may be excused
    for entering on these personal details, as I give them to show that I
    have not been hasty in coming to a decision. 
    
    eccetera...
    Nella finestra di Debug viene scritto "INTRODUCTION" e basta. Il codice nel ciclo non si ripete, ma il codice dopo il ciclo non viene comunque eseguito. Non si verifica nessun errore. E' come se l'esecuzione fosse sospesa su Next, non va né avanti né indietro e non capisco perchè. Possibili spiegazioni?

  2. #2
    Utente di HTML.it L'avatar di albgen
    Registrato dal
    Jun 2005
    Messaggi
    3,249
    la regex, cosa dovrebbe fare in teoria?
    I got the remedy

  3. #3
    Utente di HTML.it L'avatar di oregon
    Registrato dal
    Jul 2005
    residenza
    Roma
    Messaggi
    36,480
    In effetti si blocca ... se il testo si limita a

    INTRODUCTION.

    When on board HMS 'Beagle,' as naturalist, I was

    allora funziona (almeno il codice termina).

    Penso che sia dovuto alla complessità dell'espressione e/o ad un bug della classe.
    No MP tecnici (non rispondo nemmeno!), usa il forum.

  4. #4
    Utente di HTML.it L'avatar di albgen
    Registrato dal
    Jun 2005
    Messaggi
    3,249
    secondo me non è un bug.
    c'è una specie di ricorsione all'infinito, per colpa della regex stessa.
    Per questo ho chiesto prima cosa dovrebbe fare la regeex
    I got the remedy

  5. #5
    Utente di HTML.it L'avatar di oregon
    Registrato dal
    Jul 2005
    residenza
    Roma
    Messaggi
    36,480
    Originariamente inviato da albgen
    secondo me non è un bug.
    c'è una specie di ricorsione all'infinito, per colpa della regex stessa.
    Per questo ho chiesto prima cosa dovrebbe fare la regeex
    E' possibile ... non sono espertissimo di reg exp ...
    No MP tecnici (non rispondo nemmeno!), usa il forum.

  6. #6
    Volevo ottenere tutte le frasi del testo. In effetti era un errore della regex, che ho sistemato così:
    codice:
    (\.|\!|\?)(?<Word>[,;:\-_'"\s$^]+[\d\w]+[,;:\-_'"\s$^]*)+(\.|\!|\?)
    Tuttavia prende solo alcune frasi. Ad esempio, nel testo che ho postato all'inizio, rileva solamente:
    codice:
    "When on board ..."
    "On my return home ..."
    "I hope that ..."
    Ed ignora, invece, queste altre:
    codice:
    "These facts seemed to me ..."
    "After five years' work ..."
    Non riesco a capire perchè le ignora: se sostituisco il primo + con un *, si blocca ancora.

  7. #7
    Utente di HTML.it L'avatar di albgen
    Registrato dal
    Jun 2005
    Messaggi
    3,249
    cosi' ti vabene?
    ([,;:\-_'"\s$^]+[\d\w]+[,;:\-_'"\s$^]*)+(\.|\!|\?)

    sostanzilmente senza il primo gruppo
    I got the remedy

  8. #8
    Ora ho solo il portatile ed il progetto è sul fisso, perciò dovrò aspettare un po' per provarlo. Senza il gruppo iniziale non so se l'espressione prenda correttamente solo le frasi, perchè potrebbe prendere anche solo qualche parola prima del punto. E comunque anche nelle frasi citate valevano le stesse condizioni.

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.