By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,290 Members | 1,666 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,290 IT Pros & Developers. It's quick & easy.

Can't get RegEx to work, pls help

P: n/a
H
This is kind of an followup on oneof my previous questions, and it has with
RegEx to do.
I have a string containing of several words. What would a good regex
expression looklike to get one match on every word ?

For example :
String myString =" This is the string that stupid H can't split up";

// A good RegEx needed here .. So the result would look something like this
;

Match1 = This
Match2 = is
Match3 = the
Match4 = string
Match5 = that
etc etc.
All I've managed is is to get really weird matches, so please help this
annoying and frustrated newbie

TIA


Nov 15 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
You can use Regex.Split and split on Spaces. If you want every word, you
can just use the reglular old string split. For the regex (which is much
more powerful if you need it)

String[] s = Regex.Split(myString, " ");

When you iterate through the array, you'll have each element contain one of
your words. If you want things like Commas, or if you are splitting phrases
that have spaces, definitely go with Regex.Split.

HTH,

Bill

"H" <no*********************@hotmail.com> wrote in message
news:06********************@newsb.telia.net...
This is kind of an followup on oneof my previous questions, and it has with RegEx to do.
I have a string containing of several words. What would a good regex
expression looklike to get one match on every word ?

For example :
String myString =" This is the string that stupid H can't split up";

// A good RegEx needed here .. So the result would look something like this ;

Match1 = This
Match2 = is
Match3 = the
Match4 = string
Match5 = that
etc etc.
All I've managed is is to get really weird matches, so please help this
annoying and frustrated newbie

TIA

Nov 15 '05 #2

P: n/a
H

"William Ryan" <do********@nospam.comcast.net> skrev i meddelandet
news:#E**************@TK2MSFTNGP11.phx.gbl...
You can use Regex.Split and split on Spaces. If you want every word, you
can just use the reglular old string split. For the regex (which is much
more powerful if you need it)

String[] s = Regex.Split(myString, " ");

When you iterate through the array, you'll have each element contain one of your words. If you want things like Commas, or if you are splitting phrases that have spaces, definitely go with Regex.Split.


Thanks ! I tried that before, but it didn't work.. Now it works though. Must
be magic.

But it still won't work, as I want to be rid of all empty spaces. By using
your advice if there are 3 spaces in a row, one of them would be counted as
a word. Also I want to filter out characters like ? ! , . and so on.
So no hint of how the expression would look like ?


Nov 15 '05 #3

P: n/a
> But it still won't work, as I want to be rid of all empty spaces. By using
your advice if there are 3 spaces in a row, one of them would be counted as a word. Also I want to filter out characters like ? ! , . and so on.
So no hint of how the expression would look like ?


string test = " This is the string . that? stupid H can't split up";
string cleanExp = @"[\?,\.!]+";
string cleaned = Regex.Replace(test, cleanExp, " ");
string splitExp = @"\s+";
string[] split = Regex.Split(cleaned.Trim(), splitExp);
foreach (string s in split) {
Console.WriteLine(s);
}

The operation is best done in multiple steps. This way if there are any
preceeding or trailing characters other than spaces, then they can be
replaced and trimmed off, before doing a split on any white space that is
one of more characters in length. If you additionally wanted to strip out
any punctuation contained within words(such as "can't" above). Then your
first step should be to go through and replace any of those punctuation
characters with an empty string.

Steve
Nov 15 '05 #4

P: n/a
H

"Steve - DND" <steve!@!digitalnothing.com> skrev i meddelandet
news:uU**************@TK2MSFTNGP09.phx.gbl...
But it still won't work, as I want to be rid of all empty spaces. By using your advice if there are 3 spaces in a row, one of them would be counted

as
a word. Also I want to filter out characters like ? ! , . and so on.
So no hint of how the expression would look like ?


string test = " This is the string . that? stupid H can't split up";
string cleanExp = @"[\?,\.!]+";
string cleaned = Regex.Replace(test, cleanExp, " ");
string splitExp = @"\s+";
string[] split = Regex.Split(cleaned.Trim(), splitExp);
foreach (string s in split) {
Console.WriteLine(s);
}

The operation is best done in multiple steps. This way if there are any
preceeding or trailing characters other than spaces, then they can be
replaced and trimmed off, before doing a split on any white space that is
one of more characters in length. If you additionally wanted to strip out
any punctuation contained within words(such as "can't" above). Then your
first step should be to go through and replace any of those punctuation
characters with an empty string.

Steve


Excellent ! Thanks, this I can understand and work from. Thanks again !
Nov 15 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.