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 :)
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 9 years ago.
How can i get characters between two character ? Example ;
string example = "aaaaaaaaXbbbbXaaaaaa";
How can get bbbb?
You can use String.IndexOf and String.LastIndexOf methods of String class for getting the positions of X's in your string, after that you can use String.SubString method for based their positions.
string example = "aaaaaaaaXbbbbXaaaaaa";
int firstXposition = example.IndexOf("X");
int LastXposition = example.LastIndexOf("X");
Console.WriteLine(example.Substring(firstXposition + 1, LastXposition - firstXposition -1));
Output will be;
bbbb
Here is a DEMO.
You may try like this
where substring holds two parameter
first the starting point of the string after escaping the no of characters i.e 9
second is no of characters need to display i.e 4
string example = "aaaaaaaaXbbbbXaaaaaa";
string sub = input.Substring(9, 4);
Console.WriteLine("Substring: {0}", sub);
string s = "aaaaaaaaXbbbbXaaaaaa";
string[] words = s.Split('X');
now you can use the foreach loop to get whatever you want.
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 9 years ago.
I have the string "24.04.2013".
How can I recognize if the string is a date?
Thanks in advance!
Something like this?
int count="24.04.2013".Count(c => c == '.');
I think your real question should be: How do I validate a date?
You should use DateTime.TryParseExact() to validate it.
For example:
CultureInfo enDE = new CultureInfo("de-DE");
string dateString = "24.04.2013";
DateTime date;
if (DateTime.TryParseExact(dateString, "dd.MM.yyyy", enDE, DateTimeStyles.None, out date))
Console.WriteLine("Success");
else
Console.WriteLine("Failure");
You can match the string with a regular expression (RegEx). That not only gives you whether there are two points, but also if the format is correct: 2 digits, point, 2 digits, point, 4 digits.
Regex regex = new Regex("\d{2}\.\d{2}\.\d{4}");
if (regex.IsMatch(myInput, regex))
{ ... }
You can try to use
string.Split('.').Length >= 2
possible to check it via regex!
private static bool IsDateGerman(string germanDate)
{
Regex rx = new Regex(#"[0-3]?[0-9]\.[0-1]?[1-9]\.[0-9]{1,4}");
return rx.IsMatch(germanDate);
}
still didnt handle all cases like 39.19.2013
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...
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.
this is part of my program code for a piglatin translation, so I create a punctuation for my sentence but when i compile it shows the extra place within the sentence. so I do this:
private bool isPunctuation(string noo)
{
int pos = 1;
string punctuation = ",.!?";
pos = punctuation.IndexOf(' ');
while (pos >= 0)
{
pos = punctuation.IndexOf(' ', pos + 1);
//return false;
}
return true;
}
it still show the extra space. what do i need change in this code?
It's not clear how you're trying to use your method anyway, but:
Your method never uses its input (noo)
This code:
string punctuation = ",.!?";
pos = punctuation.IndexOf(' ');
... will always leave pos as -1, as ",.!?" doesn't contain a space
Your method could only exit either via an exception or by returning true; there's no way it can ever return false, as the return false; statement is commented out. (With it uncommented, you've then got a pretty odd while loop...) How were you intending to use it?
Fundamentally, it seems to me that you need to take a step back. Think about why you wanted such a method (it's unclear what it has to do with the replacing part of the question title) and exactly how it should behave.
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?