I'm looking for a Regex to validate a rating (1-10) + optional text.
Rating is a decimal, with 1 point, which can use both dot or comma separator.
Followed by an optional space + string.
Valid
7
7,5
7.5
7,5 This is my string
7.5 Hello
Invalid
7,75
11
7This is my string
7.This is my string
10.5 string
I've got this for getting the decimal values, but I'm not sure how to get the optional text behind it.
^(10|\d)([\.\,]\d{1,1})?$
Judging by your examples, the space after the initial number is not optional. Thus, the pattern you may use is
^(?:10|[1-9](?:[.,][0-9])?)(?:\s.*)?$
or - since a partial match with Regex.IsMatch is enough to validate the string - replace (?:\s.*)?$ with a negative lookahead (?!\S) that will require a whitespace or end of string after the number:
^(?:10|[1-9](?:[.,][0-9])?)(?!\S)
^^^^^^^
See the regex demo
Details:
^ - start of a string
(?:10|[1-9](?:[.,][0-9])?) - either 10 or a digit from 1 to 9 followed with an optional sequence of a , or . and any single digit and then...
(?:\s.*)?$ - an optional sequence of any whitespace followed with any chars up to the end of string - OR -
(?!\S) - a negative lookahead that fails the match if there is no non-whitespace char immediately to the right of the current position.
C# test:
var strs = new List<string> { "7","7,5","7.5","7,5 This is my string","7.5 Hello","7,75","11","7This is my string","7.This is my string","10.5 string"};
var pattern = #"^(?:10|[1-9](?:[.,][0-9])?)(?:\s.*)?$";
foreach (var s in strs)
if (Regex.IsMatch(s, pattern))
Console.WriteLine("{0} is correct.", s);
else
Console.WriteLine("{0} is invalid.", s);
Output:
7 is correct.
7,5 is correct.
7.5 is correct.
7,5 This is my string is correct.
7.5 Hello is correct.
7,75 is invalid.
11 is invalid.
7This is my string is invalid.
7.This is my string is invalid.
10.5 string is invalid.
Related
I want to extract the double value from the string that contains a specific keyword. For example:
Amount : USD 3,747,190.67
I need to extract the value "3,747,190.67" from the string above using the keyword Amount, for that I tested this pattern in different online Regex testers and it works:
(?<=\bAmount.*)(\d+\,*\.*)*
However it doesn't work on my C# code:
if (type == typeof(double))
{
double doubleVal = 0;
pattern = #"(?<=\bAmount.*)(\d+\,*\.*)*";
matchPattern = Regex.Match(textToParse, pattern);
if (matchPattern.Success)
{
double.TryParse(matchPattern.Value.ToString(), out doubleVal);
}
return doubleVal;
}
This one works:
(?<=\bAmount.*)\d+(,\d+)*(\.\d+)?
(?<=\bAmount.*) the look behind
\d+ leading digits (at least one digit)
(,\d+)* thousands groups (zero or more times)
(\.\d+)? decimals (? = optional)
Note that the regex tester says "9 matches found" for your pattern. For my pattern it says "1 match found".
The problem with your pattern is that its second part (\d+\,*\.*)* can be empty because of the * at the end. The * quantifier means zero, one or more repetitions. Therefore, the look-behind finds 8 empty entries between Amount and the number. The last of the 9 matches is the number. You can correct it by replacing the * with a +. See: regextester with *, regextester with +. You can also test it with "your" tester and switch to the table to see the detailed results.
My solution does not allow consecutive commas or points but allows numbers without thousands groups or a decimal part.
The lookbehind (?<=\bAmount.*) is always true in the example data after Amount.
The first 7 matches are empty, as (\d+\,*\.*)* can not consume a character where there is no digit, but it matches at the position as the quantifier * matches 0 or more times.
See this screenshot of the matches:
You might use
(?<=\bAmount\D*)\d{1,3}(?:,\d{3})*(?:\.\d{1,2})?\b
(?<=\bAmount\D*) Positive lookbehind, assert Amount to the left followed by optional non digits
\d{1,3} Match 1-3 digits
(?:,\d{3})* Optionally repeat , and 3 digits
(?:\.\d{1,2})? Optionally match . and 1 or 2 digits
\b A word boundary
See a .NET regex demo
For example
double doubleVal = 0;
var pattern = #"(?<=\bAmount\D*)\d{1,3}(?:,\d{3})*(?:\.\d{1,2})?\b";
var textToParse = "Amount : USD 3,747,190.67";
var matchPattern = Regex.Match(textToParse, pattern);
if (matchPattern.Success)
{
double.TryParse(matchPattern.Value.ToString(), out doubleVal);
}
Console.WriteLine(doubleVal);
Output
3747190.67
You can omit the word boundaries if needed if a partial match is also valid
(?<=Amount\D*)\d{1,3}(?:,\d{3})*(?:\.\d{1,2})?
I have a string value of:
"Drop 1.0.2.34 - Compatible with core revision 123456"
And I am trying to get the value 1.0.2.34 with the full stops from the string.
I am using Regex to try and get it, but it returns with a "" value.
Ex.
Match match = Regex.Match(string, "([0-9]*[.][0-9]*)*");
if (match.Success)
{
string version = match.Captures[0].Value;
}
I think there is something small that I am missing because it does find a match in the string, but it doesn't have a value. Can someone please help?
Your regex matches an empty string, and since you are only looking for 1 match, it returns the empty string beofre the first char.
Use
[0-9]+(?:\.[0-9]+)+
See the regex demo
Details:
[0-9]+ - 1+ digits
(?:\.[0-9]+)+ - 1+ sequences of:
\. - a dot
[0-9]+ - 1+ digits.
C#:
string version = string.Empty;
Match match = Regex.Match(string, #"[0-9]+(?:\.[0-9]+)+");
if (match.Success)
{
version = match.Value;
}
I have this particular string:
Administrationsomkostninger I -2.889 - r0.l l0
I would like to replace these characters:r,l and i with 1.
I use this expression:
([(t|r|l|i|)])
That gives me this string:
Adm1n1s11a11onsomkos1n1nge1 1 -2.889 - 10.1 10
Now i want to replace the all digits that contains a digit followed + a whitespace
so in this case only - 10.1 10 gets converted to -10.110
Try this
string input = "Administrationsomkostninger I -2.889 - r0.l l0";
string pattern = #"(?'spaces'\s){2,}";
string output = Regex.Replace(input, pattern, " ");
I want to validate that a string follows this format (using regex):
valid: 123456789 //9 digits
valid: 12-1234567 // 2 digits + dash + 7 digits
Here's an example, how I would use it:
var r = new Regex("^[1-9]\d?-\d{7}$");
Console.WriteLine(r.IsMatch("1-2-3"));
I have the regex for the format with dash, but can't figure how to include the non-dash format???
Regex regex = new Regex("^\\d{2}-?\\d{7}$");
This will accept the two formats you want: 2 digits then an optional dash and 7 numbers.
^ \d{9} | \d{2} - \d{7} $
Remove the spaces, they are there for readability.
I have the following sample string:
string s = Console.ReadLine();
s= {6} {7613023456148 } {7.040 } {56780} {Sample String}
How do I achieve the following with regex or something similar:
Remove all numbers in a line that start with 7 and are 13 digits long.
Remove all decimal numbers.
Output
s = {6} {56780} {Sample String}
You can replace the captured string use following regex with an empty string :
7\d{12}|\d\.\d+
But Note that if your numbers are within {} you need :
\b{\s*7\d{12}\s*}\b|\b{\s*\d+\.\d+\s*}\b
See demo https://regex101.com/r/dL1vF4/1
You are looking for this regex:
var s = "{6} {7613023456148 } {7.040 } {56780} {Sample String}";
s = Regex.Replace(s, #"\s*{(?:\s*7[0-9]{12}\s*|\d+\.\d+\s*)}", string.Empty);
Console.WriteLine(s); // ==> "{6} {56780} {Sample String}"
See IDEONE demo
REGEX matches 2 alternatives inside {...} that is preceded with optional whitespace (\s*):
\s*7[0-9]{12}\s* - optional whitespace followed with7`, then 12 digits and optional whitespace
\d+\.\d+\s* - 1 or more digits, a . decimal separator, again 1 or more digits, and optional whitespace.
Since all your values are inside {...}, you need no word boundaries.