tags:

views:

35

answers:

3

I want to separate my string between two ':' characters.

For example, if the input is "mypage-google-wax:press:-happy", then I want "press" out.

It can be assumed that the input doesn't contain any numeric characters.

+3  A: 

Any reason to use regular expressions at all, rather than just:

string[] bits = text.Split(':');

That's assuming I understood your question correctly... which I'm not at all sure about. Anyway, depending on what you really want to do, this might be useful to you...

Jon Skeet
+1  A: 

If you're always going to have a string in the format {stuffIDontWant}:{stuffIWant}:{moreStuffIDontWant} then String.Split() is your answer, not Regex.

To retrieve that middle value, you'd do:

string input = "stuffIDontWant:stuffIWant:moreStuffIDontWant"; //get your input
string output = ""; 
string[] parts = input.Split(':'); 
  //converts to an array of strings using the character specified as the separator
output = parts[1]; //assign the second one
return output;

Regex is good for patern matching, but, unless you're specifically looking for the word press, String.Split() is a better answer for this need.

AllenG
A: 

If you want it in regex:

  string pattern = ":([^:]+):";
  string sentence = "some text :data1: some more text :data2: text";
  foreach (Match match in Regex.Matches(sentence, pattern))
     Console.WriteLine("Found '{0}' at position {1}", 
                       match.Groups[1].Value, match.Index);
Amarghosh