Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 9th, 2012  |  syntax: None  |  size: 0.99 KB  |  hits: 10  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. How to find a string that is delimited by a certain start and end character
  2. string testingString = "stack $over| flow $stack| exchange";
  3. var pattern = @"(?$.*?|)"; // also tried @"$[^|]|"
  4. foreach (var m in System.Text.RegularExpressions.Regex.Split(testingString, pattern)) {    
  5.     Response.Write(m );
  6. }
  7. // output == stack $over| flow $stack| exchange
  8.        
  9. string testingString = @"stack $over| flow $stack| exchange";
  10.  
  11. MatchCollection result = Regex.Matches
  12.     (testingString,
  13.             @"      
  14.                 (?<=$)  # This is a lookbehind, it ensure there is a $ before the string
  15.                 [^|]*    # Match any character that is not a |
  16.                 (?=|)   # This is a lookahead,it ensures that a | is ahead the pattern
  17.             "
  18.             , RegexOptions.IgnorePatternWhitespace);
  19.  
  20. foreach (Match item in result) {
  21.     Console.WriteLine(item.ToString());
  22. }
  23.        
  24. Regex t = new Regex(@"$([^|]+)|");
  25. MatchCollection allMatches = t.Matches("stack $over| flow $stack| exchange");