This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I want to check whether my string variable contain the particular regular expression pattern or not
xxx-xx-x
(x is numerical value) using c#. If it contains then I need to return true or false.
Can anyone please help me to resolve this issue..
Use the returned value by Regex.IsMatch().
Regex regex = new Regex("[0-9]{3}-[0-9]{2}-[0-9]");
bool containsPattern = regex.IsMatch(stringToVerify);
This is the regex you are looking for
\b\d{3}-\d{2}-\d\b
\b is a boundary..If you don't use it,you would also match 111-22-345 or 111-22-3-33 which i guess you don't want to match
using System.Text.RegularExpressions;
public static bool ControlRegex(string input)
{
Match match = Regex.Match(input, #"([A-Za-z0-9\-]+)",RegexOptions.IgnoreCase);
if (match.Success)
{
return true;
}
}
You can try something like this... You have to put correct regular expression to secend parametre of Regex.Match...
You can find the correct regex with a regex program. For example "RegEx TestBed" you can download it from here; http://regextestbed.codeplex.com/releases/view/60833 with this program, you put your text in text area, and in pattern area you try to find correct regex. And below, in the the list area, and program shows you the matches according your regex, so you can try and find your correct regex...
Related
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I want to retrieve a number from a string wherever the number starts with 8,9 or 6 and length of the number should be 8 OR 9 Characters. E.g 92000000,9200 0000,9200-0000.
How about this: (?<!\d)([896]\d{3})(?:[-\s]?)(\d{4})(?!\d).
The (?:[-\s]?) eats the optional delimiters space or dash as a non-capturing group.
You get your number by concatenating the match groups 1 and 2:
var input = new string[] {
"81000000", "92000000", "9200 0000", "9200-0000"
};
var regex = new Regex (#"(?<!\d)([896]\d{3})(?:[-\s]?)(\d{4})(?!\d)");
foreach (var str in input) {
var match = regex.Match (str);
Console.WriteLine ("TEST: {0} {1} - {2}", str, match.Success,
match.Groups [1].Value + match.Groups [2].Value);
}
I have also tried (?<!\d)([896]\d{3}(?:[-\s]?)\d{4})(?!\d) and that won't remove the delimiter character from the match result.
Try the below regex:
(?<!\d)[896]\d{3}([- ]?)\d{4}(?!\d)
Try to solve using Regexpal from next time.
Cheers.
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I want to find the regex pattern to find the text between a string and a char and replace spaces in the text with _.
Example. < Node Type="Text">Event Log < /Node >
Expected output : Event_Log
Thanks in advance. Please help.
string s = "here is my text $$$ Hello World </stop>";
Match m = Regex.Match(s, "(\\$[^<]*)<");
if (m.Success)
{
Console.WriteLine(m.Groups[1].Value);
}
string str = "$$$ Hello World </stop>";
string sPattern = "[\\$]{3}([\\d\\s\\w]*)</stop>";
Match m = Regex.Match(str, sPattern, RegexOptions.IgnoreCase);
if (m.Success) {
Console.WriteLine(m.Groups(1));
}
Converted from VB code and not tested after but should be ok.
Assuming the example is correct and the text of your question wrong, you need:
\$+[^$<]*(?=<)
If it's the other way around, try this:
(?<=\$+)[^$<]*<
BTW, all questions like this can be more easily answered using a tool like this online regex tester.
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 11 years ago.
I need to extract the value 33345.002 from the following string:
"ABC(MAX)(33345.002)"
How can I perform this in C#?
I tried handling it in SQL but was picking up the (MAX) too so now I'm gonna try C#.
Thanks
.
.
.
This is the closest so far :
string temp = "YYY(33345.002)(gg)YYYY";
temp = Regex.Replace(temp, "[^.0-9]", "");
double num;
bool success = Double.TryParse(temp, out num);
if (success)
{
//do what ever to the number}
but there is a problem, some of the numbers have zeros in front of them. like: 00033.33
This is really pretty simple.
Declare the characters you want to grap [0-9]/"0123456789" as a constant in C#
loop through the string, example:
public bool TryParseDouble(string input, out double value){
if(string.IsNullorWhiteSpace(input)) return false;
const string Numbers = "0123456789.";
var numberBuilder = new StringBuilder();
foreach(char c in input) {
if(Numbers.IndexOf(c) > -1)
numberBuilder.Append(c);
}
return double.TryParse(numberBuilder.ToString(), out value);
}
Ofcoarse this could be enhanced (perhaps just parse out the first number, or return an array of doubles parsing out all numbers) - not to mention it will parse out multiple decimals which is not exactly what you want.
The same technique can be used in T-SQL as well with looping over the string, declaring the valid values then using 'in'.
EDIT: On second thought
Regex.Match(input, #"\d+(.\d+)?")
would extract double/decimal from string then you could just use double.Parse if a match is found :).
EDIT 2: Btw for some silly reason '\ .' gets escaped as '.' on Stack Overflow. Just note that the decimal in the regex is escaped (just . matches anything)
Happy coding!
You need the same regex (including the enhancements mentioned) as provided as the top answer by J-16 SDiZ for this SO: Regular expression for decimal number, but without the ^ at the beginning and $ at the end.
Next time it might be worth searching a bit on SO or Google first :)
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
i'm doing the simplest regex.match ever, i am giving the Regex.Match a pattern of one character and it returns no match at all, and i made sure the input text contains a lot of that character?
i checked all the usings.
its just very weird.
any help would be appreciated!
Thanks.
EDIT:
my sample is "doing any type of matching is simply not WORKING"
returns an empty match
Match m=Regex.Match(#"c","abcdc");
the code is compiled with no errors, so why the NO MATCHING!!
EDIT: based on your edit the issue is that you're using the parameters out of order. You need to switch the order and supply the input (string source to find a match in) then the pattern (what to match against).
In fact, this order is specified for you by the IntelliSense as depicted in this image:
It usually helps to match the naming suggested by the IntelliSense or refer to it to ensure the proper items are being passed in.
What is the character being used? Chances are you're trying to use a character that is actually a metacharacter which holds special meaning in regex.
For example:
string result = Regex.Match("$500.00", "$").Value;
The above wouldn't return anything since $ is a metacharacter that needs to be escaped:
string result1 = Regex.Match("$500.00", #"\$").Value; // or
string result2 = Regex.Match("$500.00", "\\$").Value; // or
string result3 = Regex.Match("$500.00", Regex.Escape("$")).Value;
For a list of common metacharacters that need to be escaped look at the Regex.Escape documentation.
You have the parameters in the wrong order in your example:
Match m=Regex.Match(#"c","abcdc");
This code means that you try to find the string "abcdc" in the string "c", try it the other way around and it should work better, ie:
Match m=Regex.Match("abcdc", "c");
Also, the fact that your code compiles doesn't mean that it will necessarily find a match...
Here is the documentation for Regex.Match.
I assure you, the regular expression works. I have used it many, many times.
This will put the string "d" in the variable s:
string s = System.Text.RegularExpressions.Regex.Match("asdf", "d").Value;
If that doesn't work, perhaps you have some strange culture setting that affects how strings are compared? Does System.Globalization.CultureInfo.CurrentCulture.DisplayName return an expected value?
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
Suppose the string is:
string item = "t-ewrwerwerwerwer\r-rr\wrjkwlr";
I want to Replace all - except when it is preceded by r.
So resut will be
string cleanItem = "tewrwerwerwerwer\r-rr\wrjkwlr"'
What regular expression can be used?
I think this regular expression is a little more efficient:
-(?<!r-)
Or if your language doesn’t support negative look-behind assertions, use this expression:
(^|[^r])-
and replace it by \1 (first matching group).
A replacement on (?<!r)- by an empty string should do the trick I think.
(?<!r)-
As long as your regex flavor supports zero-width look-behind, that is.