469,327 Members | 1,265 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,327 developers. It's quick & easy.

C# DirectorySearcher.Filter using a regular expression

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.
Oct 24 '08 #1
6 15414
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*)))
Oct 24 '08 #2
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?
Oct 24 '08 #3
7,872 Expert 4TB
Can you use SQL syntax and say "sn like ' Zet*'" with the single quotes? perhaps it won't do single quotes with the =?
Oct 24 '08 #4
9,735 Expert Mod 8TB
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.

Oct 24 '08 #5
7,872 Expert 4TB
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
Oct 24 '08 #6
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 :)
Oct 27 '08 #7

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by cameron | last post: by
1 post views Thread by Jay | last post: by
4 posts views Thread by Xavier | last post: by
4 posts views Thread by mike | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.