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 4 1518
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(myS tring, " ");
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************ *********@hotma il.com> wrote in message
news:06******** ************@ne wsb.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
"William Ryan" <do********@nos pam.comcast.net > skrev i meddelandet
news:#E******** ******@TK2MSFTN GP11.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(myS tring, " ");
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 ?
> 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(t est, cleanExp, " ");
string splitExp = @"\s+";
string[] split = Regex.Split(cle aned.Trim(), splitExp);
foreach (string s in split) {
Console.WriteLi ne(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
"Steve - DND" <steve!@!digita lnothing.com> skrev i meddelandet
news:uU******** ******@TK2MSFTN GP09.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(t est, cleanExp, " "); string splitExp = @"\s+"; string[] split = Regex.Split(cle aned.Trim(), splitExp); foreach (string s in split) { Console.WriteLi ne(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 ! This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 Matches = 0;
for(Match m = Regex.Match(rtfTextEdit.SelectedRtf, @"\\fcharset\d");
m.Success; m = m.NextMatch())
{
|
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 regxUserName = new Regex(@"(?<=User-Name = )\""(+)\""",
RegexOptions.None);
|
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 way to go. I tried:
regex.replace("\D", " \D ") which did not work. I don't know how to refer
to the string that was matched in forming the replacement.
|
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 but not two or more?
example
myfile.doc = good
My.file.doc = not good
if you could give an example of the expression pattern that would most
helpful.
thanks
phil
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| | |