2013-09-26 7 views
1

J'essaie d'obtenir la chaîne de message à partir de ce fichier VMG. Je veux seulement cordes après la ligne de date et avant "FIN: VBODY"Regex pour chaîne entre 2 chaînes C#

Le meilleur que je suis arrivé à ce jour est cette chaîne regex BEGIN: VBODY ([^ \ n] * \ n +) + FIN: VBODY

Quelqu'un peut-il aider à l'affiner?

N: 
TEL:+65123345 
END:VCARD 
BEGIN:VENV 
BEGIN:VBODY 
Date:8/11/2013 11:59:00 PM 
thi is a test message 
Hello this is a test message on line 2 
END:VBODY 
END:VENV 
END:VENV 
END:VMSG 
+1

Je ne pense pas que vous ayez besoin d'utiliser regex. Beaucoup plus simple de collecter n'importe quelle ligne après BEGIN: VBODY est vu jusqu'à ce que la ligne soit END: VBODY – Spaceghost

+0

Hey Spaceghost, avez-vous une idée de ce que serait un exemple de code C# pour ça? – d123

Répondre

1

Si vous souhaitez utiliser regex, vous pouvez modifier votre regex actuelle un peu, parce que le groupe $ 0 n'a pas ce que vous recherchez.

BEGIN:VBODY\n?((?:[^\n]*\n+)+?)END:VBODY 

En fait ce qui est arrivé était ([^\n]*\n+)+ transformé en (?:[^\n]*\n+)+? (tourner cette partie paresseux pourrait être plus sûr)

Et puis envelopper toute cette partie autour parens: ((?[^\n]*\n+)+?)

j'ajouté \n? avant cela pour faire la sortie un peu plus propre.


Une solution non-regex pourrait être quelque chose comme ceci:

string str = @"N: 
    TEL:+65123345 
    END:VCARD 
    BEGIN:VENV 
    BEGIN:VBODY 
    Date:8/11/2013 11:59:00 PM 
    thi is a test message 
    Hello this is a test message on line 2 
    END:VBODY 
    END:VENV 
    END:VENV 
    END:VMSG"; 

int startId = str.IndexOf("BEGIN:VBODY")+11; // 11 is the length of "BEGIN:VBODY" 
int endId = str.IndexOf("END:VBODY"); 
string result = str.Substring(startId, endId-startId); 
Console.WriteLine(result); 

Sortie:

Date:8/11/2013 11:59:00 PM 
thi is a test message 
Hello this is a test message on line 2 

ideone demo

0

Voici une solution utilisant des expressions régulières,

 string text = @"N: 
     TEL:+65123345 
     END:VCARD 
     BEGIN:VENV 
     BEGIN:VBODY 
     Date:8/11/2013 11:59:00 PM 
     thi is a test message 
     Hello this is a test message on line 2 
     END:VBODY 
     END:VENV 
     END:VENV 
     END:VMSG"; 


string pattern = @"BEGIN:VBODY(?<Value>[a-zA-Z0-9\r\n.\S\s ]*)END:VBODY";//Pattern to match text. 
Regex rgx = new Regex(pattern, RegexOptions.Multiline);//Initialize a new Regex class with the above pattern. 
Match match = rgx.Match(text);//Capture any matches. 
if (match.Success)//If a match is found. 
{ 
     string value2 = match.Groups["Value"].Value;//Capture match value. 
     MessageBox.Show(value2); 
} 

Démo here.

et maintenant une solution non-regex,

 string text = @"N: 
     TEL:+65123345 
     END:VCARD 
     BEGIN:VENV 
     BEGIN:VBODY 
     Date:8/11/2013 11:59:00 PM 
     thi is a test message 
     Hello this is a test message on line 2 
     END:VBODY 
     END:VENV 
     END:VENV 
     END:VMSG"; 

     int startindex = text.IndexOf("BEGIN:VBODY") + ("BEGIN:VBODY").Length;//The just start index of Date... 
     int length = text.IndexOf("END:VBODY") - startindex;//Length of text till END... 
     if (startindex >= 0 && length >= 1) 
     { 
      string value = text.Substring(startindex, length);//This is the text you need. 
      MessageBox.Show(value); 
     } 
     else 
     { 
      MessageBox.Show("No match found."); 
     } 

Demo here.

Espérons que ça aide.

Questions connexes