Input string is something like this: OU=TEST:This001. We need extra "This001". Best in C#.
What about :
/OU=.*?:(.*)/
Here is how it works:
OU= // Must contain OU=
. // Any character
* // Repeated but not mandatory
? // Ungreedy (lazy) (Don't try to match everything)
: // Match the colon
( // Start to capture a group
. // Any character
* // Repeated but not mandatory
) // End of the group
For the /
they're delimiters to know where the regex start and where it ends (and for adding options).
The captured group will contain This001
.
But it would be faster with a simple Substring()
.
yourString.Substring(yourString.IndexOf(":")+1);
Resources :
"OU=" smells like you're doing an Active Directory or LDAP search and responding to the results. While regex is a brilliant tool, I just wanted to make sure that you're also aware of the excellent System.DirectoryServices.Protocols classes that were made for parsing, filtering and manipulating just this sort of data.
The SearchResult, SearchResultEntry and DirectoryAttribute in particular would be the friends you might be looking for. I don't doubt that you can regex or substring as cleverly as the next guy but it's also nice to have another good tool in the toolbox.
Have you tried these classes?
if the OU=TEST:
is your requirement before the string you want to match, use this regex:
(?<=OU\s*=\s*TEST\s*:\s*).*
that regex matches any length of text after the colon, whereas any text before the colon is just a requirement.
You can replace TEST
with [A-Za-z]+
to match any text other than TEST, or you can replace TEST
with [\w]+
to match any length of any combination of alphabet and numbers.
\s*
means it might be any number of whitespaces or nothing in that position, remove it if you don't need such a check.
A solution without regex:
var str = "OU=TEST:This00:1";
var result = str.Split(new char[] { ':' }, 2)[1];
// result == This00:1
Regex vs Split vs IndexOf
Split
var str = "OU=TEST:This00:1";
var sw = new Stopwatch();
sw.Start();
var result = str.Split(new char[] { ':' }, 2)[1];
sw.Stop();
// sw.ElapsedTicks == 15
Regex
var str = "OU=TEST:This00:1";
var sw = new Stopwatch();
sw.Start();
var result = (new Regex(":(.*)", RegexOptions.Compiled)).Match(str).Groups[1];
sw.Stop();
// sw.ElapsedTicks == 7000 (Compiled)
IndexOf
var str = "OU=TEST:This00:1";
var sw = new Stopwatch();
sw.Start();
var result = str.Substring(str.IndexOf(":") + 1);
sw.Stop();
// sw.ElapsedTicks == 40
Winner: Split
Links