RegEx multiline ne marche pas.
Bonjour à tous,
J'aimerais parser des fichiers texte pour récupérer des informations dans leur entetes.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
/* =========================================================================*/
/* Date de création : 28/08/2013 */
/* Réalisateur : Eric Bryan */
/* But procédure : Récupération des factures*/
/* : 22 line description 2*/
/* Ligne description 3 */
/* -------------------------------------------------------------------------*/
/* */
/* =========================================================================*/
/* Date de modification : 28/08/2013 */
/* Réalisateur : Eric Bryan */
/* But modification : Modification 1 */
/* : Modification 2 */
/* Modification 3 */
/* : Modification 4 */
/* =========================================================================*/
/* Paramètres de la procédure -----------------------------------------*/ |
Voici mes fonctions de recherche des valeurs :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
|
//(J'aimerais que cette méthode reste générique en précisant l'index de match et l'index de group.)
public static string RegexGroupByIndex(string input, string pattern, int index, int indexGroup)
{
Regex regex = new Regex(pattern, Options);
MatchCollection matches = Regex.Matches(input, pattern);
string sReturned = null;
if (index < matches.Count)
{
if (indexGroup <= matches[index].Groups.Count)
{
sReturned = matches[index].Groups[indexGroup].Value;
}
}
return sReturned;
}
public static string RegexGroup(string input, string pattern, string name)
{
Regex regex = new Regex(pattern, Options);
Match match = regex.Match(input);
return match.Success ? match.Groups[name].Value : null;
} |
Et voici mon appel, pour rechercher la date (28/08/2013) :
Code:
1 2 3 4 5
|
string sPattern = @"\/\*\s*(?i)date\s*[de]*\s*[la]*cr[e|é|è|ë|ê]ation\s*[\:]*\s*(?<Date_Creation>.*?\s*\*\/";
string sResult = Sql_RegEx_Udf.RegexGroup(sProcContent, sPattern, "Date_Creation");
Console.WriteLine(sResult); |
Et voici mon appel, pour rechercher toutes les lignes concernant le "but de la procédure" :
J'aimerais obtenir :
"
Récupération des factures
22 line description 2
Ligne description 3
"
Code:
1 2 3 4 5
|
sPattern = @"\/\*\s*(?i)but\s*[de]*\s*[la]*proc[e|é|è|ë|ê]dure\s*[\:]*\s*(?<But_procedure>.*?\s*\*\/(\r*\n*\/\*\s*\:\s*.*?\s*\*\/)*)\r\n\/\*\s\=*\s*\*\/";
sResult = Sql_RegEx_Udf.RegexGroup(sProcContent, sPattern, "But_procedure");
Console.WriteLine(sResult); |
Puis la récupération des buts de modification :
J'aimerais obtenir :
"
Modification 1
Modification 2
Modification 3
Modification 4
"
Code:
1 2 3 4 5
|
sPattern = @"\/\*\s*(?i)but\s*[de]*\s*[la]*modification\s*\:*\s*(.*?\s*\*\/(\r*\n*\/\*\s*\:\s*.*?\*\/)*)\r\n\/\*\s[\=|\-]*\s*\*\/";
string sResult_2 = Sql_RegEx_Udf.RegexGroupByIndex(sProcContent, sPattern, 0, 1);
Console.WriteLine(sResult_2); |
Et cela ne marche pas.
Merci beaucoup par avance pour vos réponses.
Eric.