Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

getting first 3 character with trim 1

Status
Not open for further replies.

jamert

Programmer
Dec 9, 2007
80
CA
Hi i'm trying to get the first 3 characters trimmed, this is what i have any thoughts? THanks

string s = Regex.Match("(?Q){3}","@"^\s*(.*?)\s*$", "$1");
 
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]
 
hmm... then i can do something like this, in one line:
Code:
string s = Regex.Match(rdr["pc"].ToString(), @"\\w{3}", "$1");
 
Also how would i get the last 3? Thanks
 
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]
 
for the last 3:

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]
 
SqlDataReader rdr

So just so i understand the \\w acts like a Trim() ?


Thanks
 
\\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]
 
oh.. i forgot, what about getting the 4,5&6 or a string that contains 10. Something like this perhaps?

= Regex.Match(kinPH, @"\\w{3,6}", "$1");
 
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]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top