I need reg exspression what make this "123.12312" -> "123.32", or "23.323" -> "23.32" in c#.
It must be only 2 digits after point
:)
Assuming you are parsing a string and it has at least 2 digits after the point:
/[0-9]+\.[0-9]{2}/
I know you are asking about a Regex, but this seems like a better fit for Double.TryParse followed by proper formatting.
Is there a particular reason you need to use Regular Expressions? I would think it'd be better to do something like String.Format("{0:0.00}", double). You can find a list of some helpful formatting examples at http://www.csharp-examples.net/string-format-double/
I don't realy know how regex works in C# but this is my regex
([0-9]+)(?:\.([0-9]{1,2})|)[0-9]*
Group 1 will get the part before the point and group 2 (if exists) will give the part behind the point (2 digits long)
the code here will produce all the matches out of a string:
StringCollection resultList = new StringCollection();
try {
Regex regexObj = new Regex(#"([0-9]+)(?:\.([0-9]{1,2})|)[0-9]*", RegexOptions.Singleline);
Match matchResult = regexObj.Match(subjectString);
while (matchResult.Success) {
resultList.Add(matchResult.Value);
matchResult = matchResult.NextMatch();
}
} catch (ArgumentException ex) {
// Syntax error in the regular expression
}
Related
I need to make a validation in a string with positional information using regex.
Example:
020005254877841100557810AAAAAA841158891BBBB
I need to get match position 5 until 10 and has to be only numbers.
How can I do this using RegEx?
hmm does it really have to be regex?
I would have done like this.
var myString = "020005254877841100557810AAAAAA841158891BBBB";
var isValid = myString.Substring(4, 5).All(Char.IsDigit);
If you absolutely HAVE to use regex, here it is.... (Though I think you should go with something like Jonas W's answer).
Match m = Regex.Match(myString, "^.{4}\d{5}.*");
if(m.Success){
//do stuff
}
The regex means, "from the beginning of the string (^), match 4 of any character (.{4}), then five digits, (\d{5}), then however many of any other characters (.*)"
If you really feel you must use a regex this will be the one:
"^....\d{5}"
You can also do multiple checks like this:
"^....(\d{5}|\D{5})
That one will match all numbers or all non-digit characters, but not a mix or anything with whitespace.
To build on what FailedDev mentioned.
string myString = "020005254877841100557810AAAAAA841158891BBBB";
myString.Substring(5, 5); //will get the substring at index 5 to 10.
double Num;
bool isNum = double.TryParse(myString, out Num); //returns true of all numbers
Hope that helps,
I'm trying to find a String in a Textfile. I'm using the RegEx Method for that. But I need to get the Previous String, which is existing before the found String, as Method Output. How can i do it with c#? Can anyone give me some Idea?
For Example:
In that Textfile is a Line with 'routerbl router0000;'
I'm searching 'router0000;' and if i find 'router0000;' then i want to get 'routerbl'.
You have two possibilities
Match what you want using a capturing group, something like this
(\S+)\s+router0000
and you will find your result in the capturing group 1
Match your pattern and ensure the following string with a lookahead
\S+(?=\s+router0000)
this will match only the part you want.
It won't answer your question about regular expressions, but if the only rule is to return the first part of your string before a specific sub-string, then you could using something more simple than regexp:
string test = "routerbl router0000;";
string matchingValue = "router000";
int matchingValueIndex = test.IndexOf(matchingValue);
string leftPart;
if (matchingValueIndex >= 0)
{
leftPart = test.Substring(0, matchingValueIndex);
}
I can think of two ways
Use groups (routerbl) (router0000), and use the first group
Positive lookahead: routerbl (?=router000)
Use the expression (.*)(?=\srouter0000) to find the text before the router0000 text.
string resultString = null;
try
{
resultString = Regex.Match(part, #"(.*)(?=\srouter0000)", RegexOptions.Multiline).Value;
} catch (ArgumentException ex)
{
// Syntax error in the regular expression
}
Assuming that is an example of a complete line:
Regex expression = new Regex("(.*) router0000");
I want to validate first name and last name from all existing languages.
So I want to validate that there are numbers in a string.
Thanks
[\s\p{L}]
would be the correct character class for this. But of course names can contain many more characters than those (how about Tim O'Reilly or William Henry Gates III.?).
See also Falsehoods Programmers Believe About Names.
Don't even have to use regex:
string tmp = "foo";
var match = tmp.IndexOfAny("0123456789".ToCharArray()) != -1;
Just do !Regex if your validation is in a if statement.
if ( !Regex.Match ( stringToCheck, "^[0-9]+$" ).Success ) {
// TODO.
}
I just tried this one and it should do the trick:
var regex = new Regex(#"[0-9]", RegexOptions.IgnoreCase);
var m = regex.Match(stringValue);
if (m.Success)
//TODO
I have a regular expression:
12345678|[0]{8}|[1]{8}|[2]{8}|[3]{8}|[4]{8}|[5]{8}|[6]{8}|[7]{8}|[8]{8}|[9]{8}
which matches if the string contains 12345679 or 11111111 or 22222222 ... or ... 999999999.
How can I changed this to only match if NOT the above? (I am not able to just !IsMatch in the C# unfortunately)...EDIT because that is black box code to me and I am trying to set the regex in an existing config file
This will match everything...
foundMatch = Regex.IsMatch(SubjectString, #"^(?:(?!123456789|(\d)\1{7}).)*$");
unless one of the "forbidden" sequences is found in the string.
Not using !isMatch as you can see.
Edit:
Adding your second constraint can be done with a lookahead assertion:
foundMatch = Regex.IsMatch(SubjectString, #"^(?=\d{9,12})(?:(?!123456789|(\d)\1{7}).)*$");
Works perfectly
string s = "55555555";
Regex regx = new Regex(#"^(?:12345678|(\d)\1{7})$");
if (!regx.IsMatch(s)) {
Console.WriteLine("It does not match!!!");
}
else {
Console.WriteLine("it matched");
}
Console.ReadLine();
Btw. I simplified your expression a bit and added anchors
^(?:12345678|(\d)\1{7})$
The (\d)\1{7} part takes a digit \d and the \1 checks if this digit is repeated 7 more times.
Update
This regex is doing what you want
Regex regx = new Regex(#"^(?!(?:12345678|(\d)\1{7})$).*$");
First of all, you don't need any of those [] brackets; you can just do 0{8}|1{8}| etc.
Now for your problem. Try using a negative lookahead:
#"^(?:(?!123456789|(\d)\1{7}).)*$"
That should take care of your issue without using !IsMatch.
I am not able to just !IsMatch in the C# unfortunately.
Why not? What's wrong with the following solution?
bool notMatch = !Regex.Match(yourString, "^(12345678|[0]{8}|[1]{8}|[2]{8}|[3]{8}|[4]{8}|[5]{8}|[6]{8}|[7]{8}|[8]{8}|[9]{8})$");
That will match any string that contains more than just 12345678, 11111111, ..., 99999999
I need to be able to check for a pattern with | in them. For example an expression like d*|*t should return true for a string like "dtest|test".
I'm no regular expression hero so I just tried a couple of things, like:
Regex Pattern = new Regex("s*\|*d"); //unable to build because of single backslash
Regex Pattern = new Regex("s*|*d"); //argument exception error
Regex Pattern = new Regex(#"s*\|*d"); //returns true when I use "dtest" as input, so incorrect
Regex Pattern = new Regex(#"s*|*d"); //argument exception error
Regex Pattern = new Regex("s*\\|*d"); //returns true when I use "dtest" as input, so incorrect
Regex Pattern = new Regex("s*" + "\\|" + "*d"); //returns true when I use "dtest" as input, so incorrect
Regex Pattern = new Regex(#"s*\\|*d"); //argument exception error
I'm a bit out of options, what should I then use?
I mean this is a pretty basic regular expression I know, but I'm not getting it for some reason.
In regular expressions, the * means "zeros or more (the pattern before it)", e.g. a* means zero or more a, and (xy)* expects matches of the form xyxyxyxy....
To match any characters, you should use .*, i.e.
Regex Pattern = new Regex(#"s.*\|.*d");
(Also, | means "or")
Here . will match any characters[1], including |. To avoid this you need to use a character class:
new Regex(#"s[^|]*\|[^d]*d");
Here [^x] means "any character except x".
You may read http://www.regular-expressions.info/tutorial.html to learn more about RegEx.
[1]: Except a new line \n. But . will match \n if you pass the Singleline option. Well this is more advanced stuff...
A | inside a char class will be treated literally, so you can try the regex:
[|]
How about s.*\|.*d?
The problem of your tries is, that you wrote something like s* - which means: match any number of s(including 0). You need to define the characters following the s by using . like in my example. You can use \w for alphanumerical characters, only.
Try this.
string test1 = "dtest|test";
string test2 = "apple|orange";
string pattern = #"d.*?\|.*?t";
Console.WriteLine(Regex.IsMatch(test1, pattern));
Console.WriteLine(Regex.IsMatch(test2, pattern));
Regex Pattern = new Regex(#"s*\|*d"); would work, except that having |* means "0 or more pipes". So You probably want Regex Pattern = new Regex(#"s.*\|.*d");
In Javascript, if you construct
var regex = /somestuff\otherstuff/;,
then backslashes are as you'd expect. But if you construct the very same thing with the different syntax
var regex = new Regex("somestuff\\otherstuff");
then because of a weirdness in the way Javascript is parsed you have have to double all backslashes. I suspect your first attempt was correct, but you imported a new problem while solving the old in that you ran afoul of this other issue about single backslashes.