473,796 Members | 2,559 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C# DirectorySearch er.Filter using a regular expression

14 New Member
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 DirectorySearch er 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 15945
matthewaveryusa
20 New Member
I think you can try something like this. The filter uses LDAP and you can't replace it with a regex:

(|(name=Zen*)(n ame= Zen*)))
Oct 24 '08 #2
Zetten
14 New Member
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 Recognized Expert Expert
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 Recognized Expert Moderator Expert
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 Recognized Expert Expert
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 New Member
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 DirectorySearch er 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
11878
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 have <!]> in them. I would like to use XSLT to remove the <message> elements whose CDATA (the "...") matches a particular regular expression.
5
24741
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) { DirectoryEntry entry = new DirectoryEntry(LDAPPath);
4
8225
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 but the first access to the result set takes longer that to do the query in SQL and process the sql results. From my trace.axd LDAP Test Starting Search 0.000112 0.000043 LDAP Test Done Search 0.003821 0.003374 <--- fast query at .003 sec LDAP...
1
887
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 to find that users samAccountName. Perhaps I'm going about this incorrectly but I know if I could pass my searcher.filter a paramter rather than typing "cn=jason", etc this code would work, any help would be very much appreciated!: 'Get all...
0
1898
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 System.Text.RegularExpressions; using System.Web; using System.Web.Caching;
4
1780
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 ..... --comment 2 .......
4
15553
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) and hits the Fire button. The problem is that this picks up disabled accounts which I'd rather didn't happen. I basically grabbed this string straight from somewhere but don't really know what "anr " means. ds.Filter = "(&(anr=" &...
0
1686
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 results: (&(ou>="")(name=ICTS*)) Furthermore if I use the following Filter the DirectorySearcher retrieves all OU's:
2
26878
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 matching name in active directory: ================================================ Dim oroot As DirectoryEntry = New DirectoryEntry("LDAP://my.domain.local") Dim osearcher As DirectorySearcher = New DirectorySearcher(oroot) Dim oresult As...
0
9525
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10221
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10169
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9050
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7546
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5440
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5569
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3730
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2924
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.