473,396 Members | 1,775 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Can't get RegEx to work, pls help

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
4 1487
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
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
> 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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: John Baro | last post by:
I need to determine when multiple fonts are selected in a richtextbox. A font is indicated by \fcharset(N) where (N) is a number. (To the best of my knowledge) I can use this statement int...
8
by: rjb | last post by:
Hi! Could somebody have a look and help me to optimize the code below. It may look like very bad way of coding, but this stuff is very, very new for me. I've included just few lines. Regex...
2
by: Art | last post by:
Hi & help, I'm trying to parse arithmetic expressions such as 4*(7-2.1). The first thing I'm trying is to add spaces to get: 4 * ( 7 - 2.1 ). I thought that Regular Expressions would be the...
6
by: Phil Barber | last post by:
I am using Regex to validate a file name. I have everything I need except I would like the dot(.) in the filename only to appear once. My question is it possible to allow one instance of character...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.