Advertisement
Guest User

Untitled

a guest
Jul 20th, 2017
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.12 KB | None | 0 0
  1. Regex.Match("User name (sales)", @"(([^)]*))").Groups[1].Value
  2.  
  3. ( # Escaped parenthesis, means "starts with a '(' character"
  4. ( # Parentheses in a regex mean "put (capture) the stuff
  5. # in between into the Groups array"
  6. [^)] # Any character that is not a ')' character
  7. * # Zero or more occurrences of the aforementioned "non ')' char"
  8. ) # Close the capturing group
  9. ) # "Ends with a ')' character"
  10.  
  11. string input = "User name (sales)";
  12. string output = input.Split('(', ')')[1];
  13.  
  14. string s = "User name (sales)";
  15. int start = s.IndexOf("(") + 1;
  16. int end = s.IndexOf(")", start);
  17. string result = s.Substring(start, end - start);
  18.  
  19. public string GetSubstringByString(string a, string b, string c)
  20. {
  21. return c.Substring((c.IndexOf(a) + a.Length), (c.IndexOf(b) - c.IndexOf(a) - a.Length));
  22. }
  23.  
  24. GetSubstringByString("(", ")", "User name (sales)")
  25.  
  26. sales
  27.  
  28. (([a-z]+?))
  29.  
  30. Regex regex = new Regex("\((?<TextInsideBrackets>\w+)\)");
  31. string incomingValue = "Username (sales)";
  32. string insideBrackets = null;
  33. Match match = regex.Match(incomingValue);
  34. if(match.Success)
  35. {
  36. insideBrackets = match.Groups["TextInsideBrackets"].Value;
  37. }
  38.  
  39. string input = "User name (sales)";
  40.  
  41. string output = input.Substring(input.IndexOf('(') + 1, input.IndexOf(')') - input.IndexOf('(') - 1);
  42.  
  43. using System;
  44. using System.Text.RegularExpressions;
  45.  
  46. private IEnumerable<string> GetSubStrings(string input, string start, string end)
  47. {
  48. Regex r = new Regex(Regex.Escape(start) +`"(.*?)"` + Regex.Escape(end));
  49. MatchCollection matches = r.Matches(input);
  50. foreach (Match match in matches)
  51. yield return match.Groups[1].Value;
  52. }
  53.  
  54. string test = "(test)";
  55. string word = Regex.Match(test, @"((w+))").Groups[1].Value;
  56. Console.WriteLine(word);
  57.  
  58. input.Remove(input.IndexOf(')')).Substring(input.IndexOf('(') + 1);
  59.  
  60. string input= "my name is (Jayne C)";
  61. int start = input.IndexOf("(");
  62. int stop = input.IndexOf(")");
  63. string output = input.Substring(start+1, stop - start - 1);
  64.  
  65. string input = "my name is (Jayne C)";
  66. string output = input.Substring(input.IndexOf("(") +1, input.IndexOf(")")- input.IndexOf("(")- 1);
  67.  
  68. // Returns the text between 'start' and 'end'.
  69. string ExtractBetween(string text, string start, string end)
  70. {
  71. int iStart = text.IndexOf(start);
  72. iStart = (iStart == -1) ? 0 : iStart + start.Length;
  73. int iEnd = text.LastIndexOf(end);
  74. if(iEnd == -1)
  75. {
  76. iEnd = text.Length;
  77. }
  78. int len = iEnd - iStart;
  79.  
  80. return text.Substring(iStart, len);
  81. }
  82.  
  83. string result = ExtractBetween("User name (sales)", "(", ")");
  84.  
  85. string separator = "n"; //line terminator
  86.  
  87. string output;
  88. string input= "HowAreYou?nLets go there!";
  89.  
  90. output = input.Substring(0, input.IndexOf(separator));
  91.  
  92. public static string GetNestedString(this string str, char start, char end)
  93. {
  94. int s = -1;
  95. int i = -1;
  96. while (++i < str.Length)
  97. if (str[i] == start)
  98. {
  99. s = i;
  100. break;
  101. }
  102. int e = -1;
  103. while(++i < str.Length)
  104. if (str[i] == end)
  105. {
  106. e = i;
  107. break;
  108. }
  109. if (e > s)
  110. return str.Substring(s + 1, e - s - 1);
  111. return null;
  112. }
  113.  
  114. public static string GetNestedString(this string str, char start, char end)
  115. {
  116. int s = -1;
  117. int i = -1;
  118. while (++i < str.Length)
  119. if (str[i] == start)
  120. {
  121. s = i;
  122. break;
  123. }
  124. int e = -1;
  125. int depth = 0;
  126. while (++i < str.Length)
  127. if (str[i] == end)
  128. {
  129. e = i;
  130. if (depth == 0)
  131. break;
  132. else
  133. --depth;
  134. }
  135. else if (str[i] == start)
  136. ++depth;
  137. if (e > s)
  138. return str.Substring(s + 1, e - s - 1);
  139. return null;
  140. }
  141.  
  142. public String getSubstring(String startChar, String endChar, String initString)
  143. {
  144. return initString.substring((initString.indexOf(startChar) + 1), (initString.indexOf(endChar)));
  145. }
  146.  
  147. int start = input.IndexOf("(") + 1;
  148. int end = input.IndexOf(")") - start;
  149. output = input.Substring(start, end);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement