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

C# DirectorySearcher.Filter using a regular expression

14
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 15840
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
Zetten
14
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
Plater
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
Frinavale
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.

-Frinny
Oct 24 '08 #5
Plater
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
Zetten
14
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

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

Similar topics

3
by: Edwin G. Castro | last post by:
Hi, I'm new to XSLT and I'm having a hard time figuring out whether XSLT will do what I need it to do. I have a XML file with a whole bunch of <message> elements. These <message> elements...
5
by: Dave | last post by:
Hi All C# ADSI samples that I run cause unspecified errors. If I translate them into VB.NET they run fine. Here's an example: public string getEmail(string LDAPPath, string username) {...
4
by: cameron | last post by:
I have always been under the impression that LDAP was optimized for speed. Fast queries, fast access, slower writes. I have a block of data in LDAP and in SQL. Exact same data. The query is fast...
1
by: Jay | last post by:
I need to add a parameter to a directorysearcher.filter rather than using hard-coded text. I have the following code that finds all members of an AD group and then for each of those results tries...
0
by: Anonieko Ramos | last post by:
Answer. Use IHttpHandler. thanks Ro ry for coming up with this code. It processes css file to add variables. neat idea using System; using System.IO; using System.Text; using...
4
by: Xavier | last post by:
hello, in a string which i read from a textfile there are some lines, which must be eliminated. The content of the stringvariable is for example : myString=" --comment 1 Update ........
4
by: mike | last post by:
I'm needing a bit of help to fine tune my Filter property for a DirectorySearcher. My context here is for a company phone directory. A user enters the string they want to search for (in txtSearch)...
0
by: LiQuick | last post by:
Dear Reader, When I try to run the following LDAP filter with the DirectorySearcher it doesn't return any OU's but when I use it with Active Directory Users and Computers it returns the desired...
2
by: Jim in Arizona | last post by:
I'm trying to do a check to see if a specific active directory user account exists in active directory AND a specific group. I can't seem to get the filter down right. I can do this to find a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
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.