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

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

Related

set own string format using c#

I'm currently developing an application for our companys stock. In this application I have to create a format check button. So if the text in the textbox is not in the right format, it should throw an error.
I thought using String.Format() method but I think I'm doing it wrong and I don't know if it's possible using this method. I tried the following:
string format = String.Format("XXX.XX.XX.XX.X", txtStockFormat.Text);
if (format != txtStockFormat.Text)
{
MessageBox.Show("if");
}
else
{
MessageBox.Show("else");
}
I don't know if String.Format() is the right way to achieve what I want and I think I don't quite understand it right. I already searched on this site and I saw that there are loads of given formats but I couldn't found a way to set my own format.
Can someone tell me how I can achieve this?
Suggestion appreciated :)
As the comments on your question already said, you have to use Regex. I had a similar problem about 1 month ago and it took me a while to understand how Regex works.
I think you are looking for something like this:
string pattern = #"[a-zA-Z0-9][a-zA-Z0-9][a-zA-Z0-9]\.[a-zA-Z0-9][a-zA-Z0-9]\.[a-zA-Z0-9][a-zA-Z0-9]\.[a-zA-Z0-9][a-zA-Z0-9]\.[a-zA-Z0-9]";
Match match = Regex.Match(txtStockFormat.Text, pattern);
if (match.Success)
{
// Do your stuff
}
else
{
// Do your stuff
}
In your question you wanted the format XXX.XX.XX.XX.X. So let's have a look at the first three letters. In my code example the first three letters could be "a-z", "A-Z" and numbers from "0-9". If you want only uppercase letters for the first three chars you have to change the pattern to [A-Z].
string pattern = #"[a-zA-Z0-9][a-zA-Z0-9][a-zA-Z0-9]\.[a-zA-Z0-9][a-zA-Z0-9]\.[a-zA-Z0-9][a-zA-Z0-9]\.[a-zA-Z0-9][a-zA-Z0-9]\.[a-zA-Z0-9]";
^ ^ ^ ^
| | | |
1st char 2nd char 3rd char Point (separator) etc.
You can just replace the pattern I wrote above with the pattern you want. I defined the pattern for your example you posted in your question.
UPDATE:
As #Rawling already said in the comments, to make it clearer and simpler, this will have the same effect:
string pattern = #"[a-zA-Z0-9]{3}\.[a-zA-Z0-9]{2}\.[a-zA-Z0-9]{2}\.[a-zA-Z0-9]{2}\.[a-zA-Z0-9]";
Your question is not clear, as you asked to set a string format but in code you are trying to compare a text with that format.
Anyway if you want to change a string to your own desired format, you can simply use ToString method like _yourString.ToString(_yourFormat).

C# Regex Help replacing patterns

I'm not really good at regex (I only get to use it a few times a year) and want to see if someone can help with a C# regex statement which finds all instances of
<####-##-##> or </####-##-##>
and replaces it with
<date-####-##-##> or </date-####-##-##>
so that
<2012-01-01>stuff</2012-01-01><2012-05-01>stuff2</2012-05-01>
becomes
<date-2012-01-01>stuff</date-2012-01-01><date-2012-05-01>stuff2</date-2012-05-01>
string test = "<2012-01-01>stuff</2012-01-01><2012-05-01>stuff2</2012-05-01>";
var regex = new Regex(#"<(/?)(\d\d\d\d)-(\d\d)-(\d\d)>");
var result = regex.Replace(test, #"<$1date-$2-$3-$4>");
Console.WriteLine(result);
//output:
//<date-2012-01-01>stuff</date-2012-01-01><date-2012-05-01>stuff2</date-2012-05-01>
Note that the need for detail goes up depending on the other text in the strings your are processing. Are there lots of other tags? Numbers that aren't dates? etc..
If you examine the values inside tags this would be a solution.
if(Regex.IsMatch(input, #"^(19|20)\d\d[- /.](0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])$"))
{
input.Replace(input,"date-"+input);
}

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

Search string for substring

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

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