473,407 Members | 2,314 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,407 software developers and data experts.

regex question

Hi,

could someone help me putting together a regex expression for my problem?

I need my search filter to treat spaces and commas in the query the same
way no matter how many there are...

Something like this:
If a user enter "song, song" the filter has to find:
song song
song song
song,song
as well as
blasong, songbla

any tips?

thanks,
sačo
Jul 26 '06 #1
6 3915
You would have to explain the business rules of your "search filter."

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Chicken Salad Alchemist

Sequence, Selection, Iteration.
"Sačo Zagoranski" <sa**@skz.siwrote in message
news:Z7********************@news.siol.net...
Hi,

could someone help me putting together a regex expression for my problem?

I need my search filter to treat spaces and commas in the query the same
way no matter how many there are...

Something like this:
If a user enter "song, song" the filter has to find:
song song
song song
song,song
as well as
blasong, songbla

any tips?

thanks,
sačo

Jul 26 '06 #2
You would have to explain the business rules of your "search filter."
As I wrote:

Something like this:
If a user enter "song, song" the filter has to find:
song song
song song
song,song
as well as:
blasong, songbla
blasong, songbla
but not this:
song sometext song

The filter has to ignore spaces and commas between the words entered.


Jul 26 '06 #3
If the search text is being entered in a Textbox or any other visual
component then one Solution could be to capture the Keypress event and then
ignore the space and comma and just store the actual text in a variable but
display everything.
I am new to the .net but there should a method to find a particular
character in a string and retrieve the rest of the string.
"Sačo Zagoranski" <sa**@skz.siwrote in message
news:HW********************@news.siol.net...
>
You would have to explain the business rules of your "search filter."

As I wrote:

Something like this:
If a user enter "song, song" the filter has to find:
song song
song song
song,song

as well as:
blasong, songbla
blasong, songbla

but not this:
song sometext song

The filter has to ignore spaces and commas between the words entered.


Jul 26 '06 #4
* Anoop wrote, On 27-7-2006 0:09:
If the search text is being entered in a Textbox or any other visual
component then one Solution could be to capture the Keypress event and then
ignore the space and comma and just store the actual text in a variable but
display everything.
I am new to the .net but there should a method to find a particular
character in a string and retrieve the rest of the string.
"Sačo Zagoranski" <sa**@skz.siwrote in message
news:HW********************@news.siol.net...
>>You would have to explain the business rules of your "search filter."
As I wrote:

Something like this:
> If a user enter "song, song" the filter has to find:
song song
song song
song,song

as well as:
> blasong, songbla
blasong, songbla

but not this:
song sometext song

The filter has to ignore spaces and commas between the words entered.
You'll have to generate a regex on the fly to handle this:

string input = "bla bla song song...... bla bla";
// read the input from the user
string inputRegex = "song, song";
// replace space with comma, because Regex.Escape will escape whitespace
inputRegex = inputRegex.Replace(' ', ',');
// Escape the string to prevent regex injection
inputRegex = Regex.Escape(inputRegex);
// build the actual search expression
string searchExpression = Regex.Replace(inputRegex, "[, ]+", "[, ]+",
RegexOptions.None);
// find all matches in the string
MatchCollection ms = Regex.Matches(inputRegex, searchExpression);

This will do exactly what you need.

Jesse
Jul 26 '06 #5
Thanks for your reply Jesse...

You probably meant:
MatchCollection ms = Regex.Matches(input, searchExpression);
correct? Otherwise the "input" isn't used anywhere...

In any case this doesn't return any matches...
>
You'll have to generate a regex on the fly to handle this:

string input = "bla bla song song...... bla bla";
// read the input from the user
string inputRegex = "song, song";
// replace space with comma, because Regex.Escape will escape whitespace
inputRegex = inputRegex.Replace(' ', ',');
// Escape the string to prevent regex injection
inputRegex = Regex.Escape(inputRegex);
// build the actual search expression
string searchExpression = Regex.Replace(inputRegex, "[, ]+", "[, ]+",
RegexOptions.None);
// find all matches in the string
MatchCollection ms = Regex.Matches(inputRegex, searchExpression);

This will do exactly what you need.

Jesse
Jul 27 '06 #6
Sačo Zagoranski wrote:
>
>You would have to explain the business rules of your "search filter."

As I wrote:

Something like this:
If a user enter "song, song" the filter has to find:
song song
song song
song,song

as well as:
blasong, songbla
blasong, songbla

but not this:
song sometext song

The filter has to ignore spaces and commas between the words entered.
..*song(?:\ *,*\ *)song.*

Explanation from The Regulator (thanks to Roy O)

.. (any character)
* (zero or more times)
song
Non-capturing Group
' ' (space)
* (zero or more times)
,
* (zero or more times)
' ' (space)
* (zero or more times)
End Capture
song
.. (any character)
* (zero or more times)

However that will also match songsong.
Dont know if you want that or not, if not then you would have to do it
with an or so it matches one or more spaces or one or more commas (dont
know if you want song,,song to match either).

You get the idea.

JB
>

Sep 19 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: engwar1 | last post by:
Not sure where to ask this. Please suggest another newsgroup if this isn't the best place for this question. I'm new to both vb.net and regex. I need a regular expression that will validate what...
4
by: aevans1108 | last post by:
expanding this message to microsoft.public.dotnet.xml Greetings Please direct me to the right group if this is an inappropriate place to post this question. Thanks. I want to format a...
2
by: Tim Conner | last post by:
Hi, Thanks to Peter, Chris and Steven who answered my previous answer about regex to split a string. Actually, it was as easy as create a regex with the pattern "/*-+()," and most of my string...
6
by: Du Dang | last post by:
Text: ===================== <script1> ***stuff A </script1> ***more stuff <script2> ***stuff B
17
by: clintonG | last post by:
I'm using an .aspx tool I found at but as nice as the interface is I think I need to consider using others. Some can generate C# I understand. Your preferences please... <%= Clinton Gallagher ...
5
by: Chris | last post by:
How Do I use the following auto-generated code from The Regulator? '------------------------------------------------------------------------------ ' <autogenerated> ' This code was generated...
6
by: Martin Evans | last post by:
Sorry, yet another REGEX question. I've been struggling with trying to get a regular expression to do the following example in Python: Search and replace all instances of "sleeping" with "dead"....
7
by: Extremest | last post by:
I am using this regex. static Regex paranthesis = new Regex("(\\d*/\\d*)", RegexOptions.IgnoreCase); it should find everything between parenthesis that have some numbers onyl then a forward...
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...
6
by: | last post by:
Hi all, Sorry for the lengthy post but as I learned I should post concise-and-complete code. So the code belows shows that the execution of ValidateAddress consumes a lot of time. In the test...
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: 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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
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...
0
tracyyun
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...
0
isladogs
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...

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.