Can't get RegEx to work, pls help

H

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
 
W

William Ryan

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

H

William Ryan said:
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 ?
 
S

Steve - DND

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
 
H

H

Steve - DND said:
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 !
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top