So you want to get the first 3 non-white space characters from a string?
[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
string test = " 123 other stuff";
Regex re = new Regex("\\w{3}");
System.Console.WriteLine(re.Match(test));
[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
not sure what rdr is, but sure knock yourself out.
[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
string test = " 123 other stuff 456";
Regex re = new Regex("\\w{3}$");
System.Console.WriteLine(re.Match(test));
[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
\\w is the escape sequence for a character class that includes A-Za-z0-9_. If you think other characters need to be included then you can tweak it.
[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
That is a little harder to define. Do you mean the 4th, 5th, and 6th non-whitespace characters or does whitespace count for determining where to start?
[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
Well if you do just want the 4th, 5th, and 6th valid word character, then:
string test = " 123 456other stuff 789";
Regex re = new Regex("(\\w{3})(\\w{3})");
string result = re.Match(test).Groups[1].ToString();
System.Console.WriteLine(result);
[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
Oh, and just for completeness, here is hoe to get all 3 at once. I've changed it to get anything that is not white space.
string test = " 123 456other stuff 789";
Regex re = new Regex("^\\s*(\\S{3})\\s*(\\S{3}).*(\\S{3})$");
Match match = re.Match(test);
System.Console.WriteLine("First three = " + match.Groups[1].ToString());
System.Console.WriteLine("4, 5, and 6 = " + match.Groups[2].ToString());
System.Console.WriteLine("last three = " + match.Groups[3].ToString());
[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.