Connecting Tech Pros Worldwide Help | Site Map

C# DirectorySearcher.Filter using a regular expression

Newbie
 
Join Date: Aug 2008
Posts: 14
#1: Oct 24 '08
I have an AD search module which works as I want it to; searching for a matching forename and/or surname in the appropriate OU.

I would like to extend it to be more flexible, so that instead of just searching for a matching string in the surname/forename fields it can match partial strings. I already have it applying a star to the end of the filter, which accomplishes part of this, but I would like it to match partial strings at the start as well, with more precision than just a starting wildcard.

Ideally I would like it to match any record where the search term is the start of the word in question, as this will allow multi-part surnames to work without needing to code every possibility.

For example, if the user searches for "zet", it will match a field containing "Zetten" (as the actual filter passed to the DirectorySearcher object is "zet*"), but not my actual name, "van Zetten". I would like it to match my name, but not a record such as "Unzetser", so a double-star search is not suitable.

Another example: If someone searches for "Lad" it should find "bin Laden" and "Ladbroke", but not "Salad".

I think what I need to do can be accomplished with a regular expression of some kind, so it will match any whitespace character (including the start of the field being searched) at the start of the search term, rather than a completely open double-wildcard search such as "*zet*".

But I don't know if the DS.filter property can use a regex at all, and the syntax to use if it can.

I hope that's clear enough; I can try to elaborate if necessary.
Newbie
 
Join Date: Jul 2008
Posts: 20
#2: Oct 24 '08

re: C# DirectorySearcher.Filter using a regular expression


I think you can try something like this. The filter uses LDAP and you can't replace it with a regex:

(|(name=Zen*)(name= Zen*)))
Newbie
 
Join Date: Aug 2008
Posts: 14
#3: Oct 24 '08

re: C# DirectorySearcher.Filter using a regular expression


That seems like a good alternative method, although it doesn't appear to work. I think it's better than my original idea though.

Removing the pipe and searching just for
Expand|Select|Wrap|Line Numbers
  1. (sn= " + strSurname + "*)
alone (that is, with the space) works in exactly the same way as
Expand|Select|Wrap|Line Numbers
  1. (sn=" + strSurname + "*)
(without the space) so it's not very useful.

I also tried inserting single quotes around the string (including the space) but that just broke it and stopped it from returning any results at all.

It's acting like the filter object just applies a Trim() (or equivalent) to the terms, but I don't know if that's really how it's working. If it is, can it be overridden?
Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,148
#4: Oct 24 '08

re: C# DirectorySearcher.Filter using a regular expression


Can you use SQL syntax and say "sn like ' Zet*'" with the single quotes? perhaps it won't do single quotes with the =?
Frinavale's Avatar
Site Moderator
 
Join Date: Oct 2006
Location: The Great White North
Posts: 5,067
#5: Oct 24 '08

re: C# DirectorySearcher.Filter using a regular expression


Quote:

Originally Posted by Plater

Can you use SQL syntax and say "sn like ' Zet*'" with the single quotes? perhaps it won't do single quotes with the =?

I was thinking along the same lines...but was wondering if LINQ might be appropriate.

-Frinny
Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,148
#6: Oct 24 '08

re: C# DirectorySearcher.Filter using a regular expression


Actually I was thinking that if last name was "van Zetten" then you would want the merger of ("Z*" | "* Z*"), in which case the ' ' will not be ignored
Newbie
 
Join Date: Aug 2008
Posts: 14
#7: Oct 27 '08

re: C# DirectorySearcher.Filter using a regular expression


Sorry for the delay in getting back to the thread.

I'm afraid you've lost me now. Using SQL would be slightly more familiar to me, but I was under the impression that the DS.filter has its own specific syntax. Am I wrong about that? If so, how would I go about using the "sn LIKE xxx" syntax?

What is LINQ and how would that help me? I've looked it up briefly, and I think it seems to answer my question a couple of lines above, although I could be mistaken. Is it that the DS.filter has its own syntax in LINQ, which is converted to the appropriate query language/syntax based on the directory type the DirectorySearcher object is attached to?

Even if I'm right about that, I still have no idea how to actually use it to my advantage though!

Edit: Okay, I feel a bit stupid here, but I just got it working based on that very last bit. Sticking the extra star in the query with a space is working, so it's trying to match (sn=* " + strSurname + "*).

Thanks all :)
Reply