Search string for substring - c#

I need to search a large string for a particular substring. The substring will start with Testing= but everything within the double quotes could be different because its a user login.
So examples of the substring I need are
Testing="network\smithj"
or
Testing="network\rodgersm"
Do my requirements make sense? How can I do this in C#?

This is a great application of a regular expression.
"Testing=\"[^\"]*\""
You will use it like so:
Regex reg = new Regex("Testing=\"[^\"]*\"");
string login = reg.Match(yourInputString).Groups[0].Value;
The above works with your two given test cases.
Wikipedia has a great article on Regular Expressions if you are not familiar with them. And if you search google you can find a wealth of info on how to use Regular Expressions in C#.

Something like:
const string Prefix = "Testing=\"";
static string FindTestingSubstring(string text)
{
int start = text.IndexOf(Prefix);
if (start == -1)
{
return null; // Or throw an exception
}
int end = text.IndexOf('\"', start + Prefix.Length);
if (end == -1)
{
return null; // Or throw an exception
}
return text.Substring(start + Prefix.Length, end - start - Prefix.Length);
}
An alternative is to use a regular expression - but when the pattern is reasonably simple, I personally prefer simple string manipulation. It depends on how comfortable you are with regexes though :)

If the string you're searching is very large, you might not want to use regular expressions. Regexes are relatively slow at matching and will typically examine every character. Instead, lookup the Boyer-Moore string matching algorithm, which typically examines only a fraction of the characters. The CLR implementation for string.IndexOf(string) may or may not use this algorithm - you'd have to check.
Ah, here's a useful link with some benchmark results: http://www.arstdesign.com/articles/fastsearch.html

Related

Get a part of that String C#

:barbosza!barbosza#barbosza.tmi.twitch.tv PRIVMSG #pggamesbr :My text
I want the part after the second ':', but i can't split by ':' because sometimes contains it too.
You can split and specify the maximum number of items, so that everything after the second colon ends up in the third item:
string[] parts = str.Split(new char[]{':'}, 3);
The part after the second colon is now in parts[2].
I guess that "he contains it too" means "My text contains it too".
In that case, do this
string toFind = "#pggamesbr :";
string myText = myString.Substring(myString.IndexOf(toFind) + toFind.Length);
I like Guffa's simple Split solution, and would go with that if this is all you need here. But, just for fun...
If you run into a lot of odd cases like this -- things you wish were easier to do with strings -- you can consider adding extension methods to handle them. E.g.,
using System;
public static MyStringExtentions
{
public static string After(this string orig, char delimiter)
{
int p = orig.indexOf(delimiter);
if (p == -1)
return string.Empty;
else
return orig.Substring(p + 1);
}
}
And then, in your existing code, as long as you have a using directive to include reference access to MyStringExtentions's definition:
string afterPart = myString.After(':').After(':');
Disclaimer: I didn't actually test this. Some tuning may be required. And it could probably be tuned to be more efficient, etc.
Again, this is probably overkill for this one problem. (See Guffa's perfectly good simple answer for that.) Just tossing it out for when you find yourself with lots of these and want a common way to make them available.
Ref. Extension Methods (C# Programming Guide)

C# - Straight-up answer for using Regex to compare strings?

I cannot understand the syntax at all! I feel like the stackoverflow community should get a simple solution to the problem I have (hopefully I'm not blind and have missed one of the thousands of regex questions here):
string myString = "$randomText$";
string myString2 = "$otherStuff$";
What would I use to check if any string has "$*$"? So as long as there's 2 $'s on the outside of the text?
Again, I'm sorry, and I Do understand there's other regex answers out there, but there's no way I'll ever understand it. I apologize, and have a good day.
You can do this by using this:
string myString = "$randomText$";
var match = Regex.Match(myString , #"\$.+\$");
You have two options. Use regex or don't. You could either substr the input string and check the first and last char are the $ sign, or you could use regex. If you're new and don't understand regex (learn to love it, a little goes a long way) then go with the substring checks. You may also find that substringing is slightly faster than regex.
For those that want spoonfeeding (untested code). This will be (most likely) be faster than any regex check:
public bool DollahCheck(string inp, string stringToCheck = "$")
{
return inp.Substring(0,1) == stringToCheck && inp.Substring(inp.Length - 1) == stringToCheck;
}
use this
bool matched = Regex.Match(myString , #"yourRegexPattern").Success

How to Extract the Word Following a Symbol?

I have a string that could have any sentence in it but somewhere in that string will be the # symbol, followed by an attached word, sort of like #username you see on some sites.
so maybe the string is "hey how are you" or it's "#john hey how are you".
IF there's an "#" in the string i want to pull what comes immediately after it into its own new string.
in this instance how can i pull "john" into a different string so i could theoretically notify this person of his new message? i'm trying to play with string.contains or .replace but i'm pretty new and having a hard time.
this btw is in c# asp.net
You can use the Substring and IndexOf methods together to achieve this.
I hope this helps.
Thanks,
Damian
Here's how you do it without regex:
string s = "hi there #john how are you";
string getTag(string s)
{
int atSign = s.IndexOf("#");
if (atSign == -1) return "";
// start at #, stop at sentence or phrase end
// I'm assuming this is English, of course
// so we leave in ' and -
int wordEnd = s.IndexOfAny(" .,;:!?", atSign);
if (wordEnd > -1)
return s.Substring(atSign, wordEnd - atSign);
else
return s.Substring(atSign);
}
You should really learn regular expressions. This will work for you:
using System.Text.RegularExpressions;
var res = Regex.Match("hey #john how are you", #"#(\S+)");
if (res.Success)
{
//john
var name = res.Groups[1].Value;
}
Finds the first occurrence. If you want to find all you can use Regex.Matches. \S means anything else than a whitespace. This means it also make hey #john, how are you => john, and #john123 => john123 which may be wrong. Maybe [a-zA-Z] or similar would suit you better (depends on which characters the usernames is made of). If you would give more examples, I could tune it :)
I can recommend this page:
http://www.regular-expressions.info/
and this tool where you can test your statements:
http://regexlib.com/RESilverlight.aspx
The best way to solve this is using Regular Expressions. You can find a great resource here.
Using RegEx, you can search for the pattern you are after. I always have to refer to some documentation to write one...
Here is a pattern to start with - "#(\w+)" - the # will get matched, and then the parentheses will indicate that you want what comes after. The "\w" means you want only word characters to match (a-z or A-Z), and the "+" indicates that there should be one or more word characters in a row.
You can try Regex...
I think will be something like this
string userName = Regex.Match(yourString, "#(.+)\\s").Groups[1].Value;
RegularExpressions. Dont know C#, but the RegEx would be
/(#[\w]+) / - Everything in the parans is captured in a special variable, or attached to RegEx object.
Use this:
var r = new Regex(#"#\w+");
foreach (Match m in r.Matches(stringToSearch))
DoSomething(m.Value);
DoSomething(string foundName) is a function that handles name (found after #).
This will find all #names in stringToSearch

Getting rid of multiple periods in a filename using RegEx

I have an application that requires me to "clean" "dirty" filenames.
I was wondering if anybody knew how to handle files that are named like:
1.0.1.21 -- Confidential...doc
or
Accounting.Files.doc
Basically there's no guarantee that the periods will be in the same place for every file name. I was hoping to recurse through a drive, search for periods in the filename itself (minus the extension), remove the period and then append the extension onto it.
Does anybody know either a better way to do this or how do perform what I'm hoping to do?
As a note, regEx is a REQUIREMENT for this project.
EDIT: Instead of seeing 1.0.1.21 -- Confidential...doc, I'd like to see: 10121 -- Confidential.doc
For the other filename, Instead of Accounting.Files.doc, i'd like to see AccountingFiles.doc
You could do it with a regular expression:
string s = "1.0.1.21 -- Confidential...doc";
s = Regex.Replace(s, #"\.(?=.*\.)", "");
Console.WriteLine(s);
Result:
10121 -- Confidential.doc
The regular expression can be broken down as follows:
\. match a literal dot
(?= start a lookahead
.* any characters
\. another dot
) close the lookahead
Or in plain English: remove every dot that has at least one dot after it.
It would be cleaner to use the built in methods for handling file names and extensions, so if you could somehow remove the requirement that it must be regular expressions I think it would make the solution even better.
Here is an alternate solution that doesn't use regular expressions -- perhaps it is more readable:
string s = "1.0.1.21 -- Confidential...doc";
int extensionPoint = s.LastIndexOf(".");
if (extensionPoint < 0) {
extensionPoint = s.Length;
}
string nameWithoutDots = s.Substring(0, extensionPoint).Replace(".", "");
string extension = s.Substring(extensionPoint);
Console.WriteLine(nameWithoutDots + extension);
I'd do this without regular expressions*. (Disclaimer: I'm not good with regular expressions, so that might be why.)
Consider this option.
string RemovePeriodsFromFilename(string fullPath)
{
string dir = Path.GetDirectoryName(fullPath);
string filename = Path.GetFileNameWithoutExtension(fullPath);
string sanitized = filename.Replace(".", string.Empty);
string ext = Path.GetExtension(fullPath);
return Path.Combine(dir, sanitized + ext);
}
* Whoops, looks like you said using regular expressions was a requirement. Never mind! (Though I have to ask: why?)

C# Extracting a name from a string

I want to extract 'James\, Brown' from the string below but I don't always know what the name will be. The comma is causing me some difficuly so what would you suggest to extract James\, Brown?
OU=James\, Brown,OU=Test,DC=Internal,DC=Net
Thanks
A regex is likely your best approach
static string ParseName(string arg) {
var regex = new Regex(#"^OU=([a-zA-Z\\]+\,\s+[a-zA-Z\\]+)\,.*$");
var match = regex.Match(arg);
return match.Groups[1].Value;
}
You can use a regex:
string input = #"OU=James\, Brown,OU=Test,DC=Internal,DC=Net";
Match m = Regex.Match(input, "^OU=(.*?),OU=.*$");
Console.WriteLine(m.Groups[1].Value);
A quite brittle way to do this might be...
string name = #"OU=James\, Brown,OU=Test,DC=Internal,DC=Net";
string[] splitUp = name.Split("=".ToCharArray(),3);
string namePart = splitUp[1].Replace(",OU","");
Console.WriteLine(namePart);
I wouldn't necessarily advocate this method, but I've just come back from a departmental Christmas lyunch and my brain is not fully engaged yet.
I'd start off with a regex to split up the groups:
Regex rx = new Regex(#"(?<!\\),");
String test = "OU=James\\, Brown,OU=Test,DC=Internal,DC=Net";
String[] segments = rx.Split(test);
But from there I would split up the parameters in the array by splitting them up manually, so that you don't have to use a regex that depends on more than the separator character used. Since this looks like an LDAP query, it might not matter if you always look at params[0], but there is a chance that the name might be set as "CN=". You can cover both cases by just reading the query like this:
String name = segments[0].Split('=', 2)[1];
That looks suspiciously like an LDAP or Active Directory distinguished name formatted according to RFC 2253/4514.
Unless you're working with well known names and/or are okay with a fragile hackaround (like the regex solutions) - then you should start by reading the spec.
If you, like me, generally hate implementing code according to RFCs - then hope this guy did a better job following the spec than you would. At least he claims to be 2253 compliant.
If the slash is always there, I would look at potentially using RegEx to do the match, you can use a match group for the last and first names.
^OU=([a-zA-Z])\,\s([a-zA-Z])
That RegEx will match names that include characters only, you will need to refine it a bit for better matching for the non-standard names. Here is a RegEx tester to help you along the way if you go this route.
Replace \, with your own preferred magic string (perhaps & #44;), split on remaining commas or search til the first comma, then replace your magic string with a single comma.
i.e. Something like:
string originalStr = #"OU=James\, Brown,OU=Test,DC=Internal,DC=Net";
string replacedStr = originalStr.Replace("\,", ",");
string name = replacedStr.Substring(0, replacedStr.IndexOf(","));
Console.WriteLine(name.Replace(",", ","));
Assuming you're running in Windows, use PInvoke with DsUnquoteRdnValueW. For code, see my answer to another question: https://stackoverflow.com/a/11091804/628981
If the format is always the same:
string line = GetStringFromWherever();
int start = line.IndexOf("=") + 1;//+1 to get start of name
int end = line.IndexOf("OU=",start) -1; //-1 to remove comma
string name = line.Substring(start, end - start);
Forgive if syntax is not quite right - from memory. Obviously this is not very robust and fails if the format ever changes.

Categories

Resources