Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Regex.Match("User name (sales)", @"(([^)]*))").Groups[1].Value
- ( # Escaped parenthesis, means "starts with a '(' character"
- ( # Parentheses in a regex mean "put (capture) the stuff
- # in between into the Groups array"
- [^)] # Any character that is not a ')' character
- * # Zero or more occurrences of the aforementioned "non ')' char"
- ) # Close the capturing group
- ) # "Ends with a ')' character"
- string input = "User name (sales)";
- string output = input.Split('(', ')')[1];
- string s = "User name (sales)";
- int start = s.IndexOf("(") + 1;
- int end = s.IndexOf(")", start);
- string result = s.Substring(start, end - start);
- public string GetSubstringByString(string a, string b, string c)
- {
- return c.Substring((c.IndexOf(a) + a.Length), (c.IndexOf(b) - c.IndexOf(a) - a.Length));
- }
- GetSubstringByString("(", ")", "User name (sales)")
- sales
- (([a-z]+?))
- Regex regex = new Regex("\((?<TextInsideBrackets>\w+)\)");
- string incomingValue = "Username (sales)";
- string insideBrackets = null;
- Match match = regex.Match(incomingValue);
- if(match.Success)
- {
- insideBrackets = match.Groups["TextInsideBrackets"].Value;
- }
- string input = "User name (sales)";
- string output = input.Substring(input.IndexOf('(') + 1, input.IndexOf(')') - input.IndexOf('(') - 1);
- using System;
- using System.Text.RegularExpressions;
- private IEnumerable<string> GetSubStrings(string input, string start, string end)
- {
- Regex r = new Regex(Regex.Escape(start) +`"(.*?)"` + Regex.Escape(end));
- MatchCollection matches = r.Matches(input);
- foreach (Match match in matches)
- yield return match.Groups[1].Value;
- }
- string test = "(test)";
- string word = Regex.Match(test, @"((w+))").Groups[1].Value;
- Console.WriteLine(word);
- input.Remove(input.IndexOf(')')).Substring(input.IndexOf('(') + 1);
- string input= "my name is (Jayne C)";
- int start = input.IndexOf("(");
- int stop = input.IndexOf(")");
- string output = input.Substring(start+1, stop - start - 1);
- string input = "my name is (Jayne C)";
- string output = input.Substring(input.IndexOf("(") +1, input.IndexOf(")")- input.IndexOf("(")- 1);
- // Returns the text between 'start' and 'end'.
- string ExtractBetween(string text, string start, string end)
- {
- int iStart = text.IndexOf(start);
- iStart = (iStart == -1) ? 0 : iStart + start.Length;
- int iEnd = text.LastIndexOf(end);
- if(iEnd == -1)
- {
- iEnd = text.Length;
- }
- int len = iEnd - iStart;
- return text.Substring(iStart, len);
- }
- string result = ExtractBetween("User name (sales)", "(", ")");
- string separator = "n"; //line terminator
- string output;
- string input= "HowAreYou?nLets go there!";
- output = input.Substring(0, input.IndexOf(separator));
- public static string GetNestedString(this string str, char start, char end)
- {
- int s = -1;
- int i = -1;
- while (++i < str.Length)
- if (str[i] == start)
- {
- s = i;
- break;
- }
- int e = -1;
- while(++i < str.Length)
- if (str[i] == end)
- {
- e = i;
- break;
- }
- if (e > s)
- return str.Substring(s + 1, e - s - 1);
- return null;
- }
- public static string GetNestedString(this string str, char start, char end)
- {
- int s = -1;
- int i = -1;
- while (++i < str.Length)
- if (str[i] == start)
- {
- s = i;
- break;
- }
- int e = -1;
- int depth = 0;
- while (++i < str.Length)
- if (str[i] == end)
- {
- e = i;
- if (depth == 0)
- break;
- else
- --depth;
- }
- else if (str[i] == start)
- ++depth;
- if (e > s)
- return str.Substring(s + 1, e - s - 1);
- return null;
- }
- public String getSubstring(String startChar, String endChar, String initString)
- {
- return initString.substring((initString.indexOf(startChar) + 1), (initString.indexOf(endChar)));
- }
- int start = input.IndexOf("(") + 1;
- int end = input.IndexOf(")") - start;
- output = input.Substring(start, end);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement