RegEx for limited decimal - c#

New to regex syntax here. Trying to write a regex to provide some input validation.
What I need is a regex to match a whole number, or a decimal with exactly one digit past the decimal.
Good Match
1
12
100
1.1
100.1
1.0
No Match
1.22
1.
0
012
Here is what I came up with but it doesn't work:
Regex.IsMatch(paidHours, "\\d+[.]?[0-9]?")

You can try with:
Regex.IsMatch(paidHours, "^\\d+(\\.\\d)?$")

Regex.IsMatch(paidHours, #"^\d+(\.\d)?$")

Edited answer after OP question edit.
Regex.IsMatch(paidHours, #"^[1-9][0-9]*(\.[0-9])?$");
Explanation:
^ : Start of the String
[1-9] : A single number between 1 and 9
[0-9]* : Zero or more number(s) between 0 and 9
([0-9]? would match zero or one number and the String "100" would not match the regex)
( : Start of a group
\. : A point
[0-9] : A single number between 0 and 9
)? : End of the group. The group must be repeated zero or one time
$ : End of the String
Please note that \d is not exactly equivalent to [0-9]: \d matches any unicode digit. For instance, this character ௮ will be matched if you use \d but won't be if you use [0-9].

Try to specify beggining/end of the line:
#"^\d+[.]?[0-9]?$"
You regex won't work since ie 1.234 was a match wit 1.2, if you don't specify you want the string ends with the '$' sign.

Related

C# regex string that is not another string

I want to match an at least 3 letter word, preceded by any character from class [-_ :] any amount of times, that is not this specific 3 letter word string2.
Ex:
if string2="VER"
in
" ODO VER7"
matched " ODO"
or
"_::ATTPQ VER7"
matched "_::ATTPQ"
but if
" VER7"
it shoudn't match " VER"
so I thought about
Regex.Match(inputString, #"[-_:]*[A-Z]{3,}[^(VER)]", RegexOptions.IgnoreCase);
where
[-_:]* checks for any character in class, appearing 0 or more times
[A-Z] the range of letters that could form the word
{3,} the minimum amount of letters to form the word
[^(VER)] the grouping construct that shouldn't appear
I believe however that [A-Z]{3,} results in any letter at least 3 times (not what i want)
and [^(VER)] not sure what it's doing
Using [^(VER)] means a negated character class where you would match any character except ( ) V E or R
For you example data, you could match 0+ spaces or tabs (or use \s to also match a newline).
Then use a negative lookahead before matching 3 or more times A-Z to assert what is on the right is not VER.
If that is the case, match 3 or more times A-Z followed by a space and VER itself.
^[ \t]*[-_:]*(?!VER)[A-Z]{3,} VER
Regex demo
^\s*[-_:]*(?!VER)[A-Z]{3,}
This regex asserts that between the start and end of the string, there's zero or more of your characters, followed by at least 3 letters. It uses a negative lookahead to make sure that VER (or whatever you want) is not present.
Demo
This would match the preceding class characters [-_ :] of 3 or more letters/numbers
that do not start with VER (as in the samples given) :
[-_ :]+(?!VER)[^\W_]{3,}
https://regex101.com/r/wLw23I/1

regex to match only decimals after a colon

I'm real close but I'm not a regex expert. Here's my input strings.
DCIN : 11.896V
5V : 4.988V
Vcom : 0.008V
5VStby: 4.992V
48V : 0.042V
48I : 0mA
I want only the numeric values after the colon. This is what I have so far
/[^\D]+\.?[^\D]+/
and it's also grabbing the two 48 instances and it isn't getting the 0
Your regex requires at least 2 digits on end, that is why it does not match the zero on the last line, and there is no restriction in your pattern to only match after a colon.
Use
var res = Regex.Matches(s, #":\s*(\d*\.?\d+)")
.Cast<Match>()
.Select(m=>m.Groups[1].Valu‌​e)
.ToList();
See the regex demo
Details:
:\s* - a colon and zero or more whitespace
(\d*\.?\d+) - Capturing group 1 holding the value you need:
\d* - zero or more digits
\.? - an optional dot
\d+ - one or more digits.
I'd do this
^[^:]*:\s*(\d+(?:\.\d*)?)

Replace [0-9] with 0[0-9]

I would like to know if there is a way to add a 0 in front of every 1 digit number using regex.
The input string is a list of 1-3 numbers separated by a '-' (i.e. 28, 12-56, 67-6, 45-65-46).
The two issues I have come across are:
Matching all the possible 1 digit numbers without doing a seperate check for each of the below: ^[0-9]-, -[0-9]-, ^[0-9]&, -[0-9]&
Adding the 0 without removing anything else. So turn: Regex.Replace(input, "^[0-9]-","0") into something like: Regex.Replace(input, "^[0-9]-","^0[0-9]-")
search: (?<!\d)(\d)(?!\d)
replace 0$1
Where:
(?<!\d) is a negative lookbehind that makes sure we haven't a digit before the match.
(\d) is a capture group, that is referenced by $1 in the replacement part.
(?!\d) is a negative lookahead that makes sure we haven't a digit after the match.
See lookaround for info.
This replaces digit not preceeded or followed by other digit by 0 + the digit.
Regex.Replace(input, #"(?<!\d)(\d)(?!\d)","0$1")

Regular expression for decimal numbers with limiting character size

I'm trying to frame regular expression that matches the following criteria. Can you please suggest me a solution.
I tried with : ^[0-9]+(\.[0-9]{1,2})?$
It is working as expected but now i want to add a pattern to limit the character size up to 10(max) including period('.') in the text box.
Acceptance Criteria Values (When Char length is 6) :
.5, 0.5, 1.2, 33.33, 123.45
Rejection Criteria Values (When Char length is 6) :
.5.0, -0.5, 2.333, 122.456, 1234.56 (rejected because the length is 7 including dot ('.')
One way to do it is with a positive look-ahead. First check the condition then match:
string pattern = #"(?x) (?=^.{1,10}$ ) (^ \d+ (\.\d{1,2})? $) | (^\.\d{1,2}$)";
(?=^.{1,10}$ ) is the look-ahead, matches a string from 1 to 10 inclusive, then you match :
(^ \d+ (\.\d{1,2})? $) matches any digit followed by . and 1 or 2 other digits
| or
(^\.\d{1,2}$) matches . followed by 1 or 2 digits
Thanks,
I got answer like splitting into two regular expressions. One for numeric validation and another for length validation. If both conditions match then it is said as accepted.
Thanks for your valuable suggestions.
Naveen

How to make this regex allow spaces c#

I have a phone number field with the following regex:
[RegularExpression(#"^[0-9]{10,10}$")]
This checks input is exactly 10 numeric characters, how should I change this regex to allow spaces to make all the following examples validate
1234567890
12 34567890
123 456 7890
cheers!
This works:
^(?:\s*\d\s*){10,10}$
Explanation:
^ - start line
(?: - start noncapturing group
\s* - any spaces
\d - a digit
\s* - any spaces
) - end noncapturing group
{10,10} - repeat exactly 10 times
$ - end line
This way of constructing this regex is also fairly extensible in case you will have to ignore any other characters.
Use this:
^([\s]*\d){10}\s*$
I cheated :) I just modified this regex here:
Regular expression to count number of commas in a string
I tested. It works fine for me.
Use this simple regex
var matches = Regex.Matches(inputString, #"([\s\d]{10})");
EDIT
var matches = Regex.Matches(inputString, #"^((?:\s*\d){10})$");
explain:
^ the beginning of the string
(?: ){10} group, but do not capture (10 times):
\s* whitespace (0 or more times, matching the most amount possible)
\d digits (0-9)
$ before an optional \n, and the end of the string
Depending on your problem, you might consider using a Match Evaluator delegate, as described in http://msdn.microsoft.com/en-us/library/system.text.regularexpressions.matchevaluator.aspx
That would make short work of the issue of counting digits and/or spaces
Something like this i think ^\d{2}\s?\d\s?\d{3}\s?\d{4}$
There are variants : 10 digits or 2 digits space 8 digits or 3 digits space 3 digits space 4 digits.
But if you want only this 3 variants use something like this
^(?:\d{10})|(?:\d{2}\s\d{8})|(?:\d{3}\s\d{3}\s\d{4})$

Categories

Resources