regex question

  • Thread starter =?ISO-8859-2?Q?Sa=B9o_Zagoranski?=
  • Start date
?

=?ISO-8859-2?Q?Sa=B9o_Zagoranski?=

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
 
K

Kevin Spencer

You would have to explain the business rules of your "search filter."

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Chicken Salad Alchemist

Sequence, Selection, Iteration.
 
?

=?ISO-8859-2?Q?Sa=B9o_Zagoranski?=

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.
 
A

Anoop

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.
 
J

Jesse Houwing

* 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.

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
 
?

=?windows-1252?Q?Sa=9Ao_Zagoranski?=

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...
 
J

John B

Sa¹o Zagoranski said:
As I wrote:

Something like this:

as well as:

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
 

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

Top