Come eseguire la ricerca su più righe usando l'espressione regolare?
Domanda
Sono nuovo alle espressioni regolari.
Voglio fare una ricerca su più righe. Ecco l'esempio di ciò che voglio fare:
Supponiamo di avere il seguente testo:
*Project #1: CVC – Customer Value Creation (Sep 2007 – till now) Time Warner Cable is the world's leading media and entertainment company, Time Warner Cable (TWC) makes coaxial quiver. Client : Time Warner Cable, US. ETL Tool : Informatica 7.1.4 Database : Oracle 9i. Role : ETL Developer/Team Lead. O/S : UNIX. Responsibilities: Created Test Plan and Test Case Book. Peer reviewed team members Mappings. Documented Mappings. Leading the Development Team. Sending Reports to onsite. Bug fixing for Defects, Data and Performance related. Project #2: MYER – Sales Analysis system (Nov 2005 – till now) Coles Myer is one of Australia's largest retailers with more than 2,000 stores throughout Australia, Client : Coles Myer Retail, Australia. ETL Tool : Informatica 7.1.3 Database : Oracle 8i. Role : ETL Developer. O/S : UNIX. Responsibilities: Extraction, Transformation and Loading of the data using Informatica. Understanding the entire source system. Created and Run Sessions and Workflows. Created Sort files using Syncsort Application.*
Voglio scrivere RegEx che dovrebbe prima provare a far corrispondere la parola "Progetto" che può essere in maiuscolo o minuscolo.
Se " progetto " corrispondenze, quindi RegEx dovrebbe cercare di abbinare client, ruolo, ambiente. Se RegEx. corrisponde a QUALUNQUE di questi, quindi la corrispondenza è completa. (Le parole cliente, ruolo, ambiente possono essere in ogni caso anche loro possono o meno essere sulla stessa linea di quella della parola "progetto")
Ho scritto un'espressione regolare per l'attività precedente che è così:
^((P|p)roject.*\s*.*((((E|e)nviornment)|((P|p)latform)|((R|r)ole(s)?)|((R|r)esponsibilit(y|ies))|((C|c)lient)|((C|c)ustomer)|((P|p)eriod)))
Questo RegEx. corrisponde al Progetto n. 1 ma non corrisponde al Progetto n. 2.
Qualcuno può dirmi cosa c'è di sbagliato in questo RegEx o come scrivere RegEx per questo tipo di testo?
Soluzione
Prova questo:
Regex project = new Regex(
@"^(Project [\s\S]*?" +
@"(Environment|Platform|Roles?|Responsibilit(y|ies)|Client|Customer|Period))",
RegexOptions.ECMAScript | RegexOptions.IgnoreCase | RegexOptions.Multiline);
Altri suggerimenti
In caso di C # è possibile specificare le opzioni multilinea come parametro per il costruttore Regex:
Regex r = new Regex("(var matches = new Array\\([^\\)]*\\);)",
RegexOptions.IgnoreCase | RegexOptions.Compiled
| RegexOptions.Multiline);
Per ulteriori dettagli sul codice, fare riferimento al collegamento: C # e Regex: come estrarre stringhe tra virgolette
poiché non hai specificato un linguaggio di programmazione, ecco alcuni schemi comunemente usati per realizzare questo
/yourRegexpattern/m <-- the m stays for multiline
puoi anche usare
/yourRegexpattern/im <-- the i stays for case insensitivity
per rimuovere la necessità di quei (P | p)
ecc.
In C #, devi specificare questi flag nel costruttore di regex, basta usare il completamento automatico.