tags:

views:

63

answers:

2

I have a list of alpha-numeric values that I need to search through using a "greater than or equal to" and/or "less than or equal to" operator.

Example list of values to be searched:
a
b
c
d
e
f

User enters b, and specifies to return everything "greater than or equal to" it.

Expected results:
b
c
d
e
f

What is the regular expression for this?

Edit: I'm suggesting regex because the users have the ability to enter wildcards.

P.S. This is not homework :)

Thanks in advance!

+4  A: 

This isn't something you should be using regular expressions for.

Iterate over the list and examine each item to see if is "greater than or equal to" the item the user provided. If you are using .NET 3.5 then you might find LINQ useful, and in particular the Enumerable.Where extension method.

If you really want to use a regular expression you could use [_-f] where the underscore must be replaced by the letter the user enters, but this only works on your specific example. Generalizing it to work with any strings of any length would be more complicated (and the resulting regular expression would be very messy).

Mark Byers
Thank you Mark!
deadbug
A: 

An alternative to another answers would be to keep your list of values sorted, and find the appropriate position using binary search. Be warned however that for the value lists this can be actually slower then just comparing everything in the list to the threshold value.

Vlad