Regular expression for characters after '.' [duplicate] - c#

This question already has answers here:
How do I match an entire string with a regex?
(8 answers)
Closed 6 years ago.
I need to detect following format when I enter serial number like
CK123456.789
I used Regex with pattern of
^(CV[0-9]{6}\.[0-9]{3}
to match but if I enter
CK123456.7890
it still able to proceed without flagging error. Is there a better regular expression to detect the trailing 3 digits after '.'?

Depending on how you use the regular expression matcher, you might need to enclose it in ^...$ which forces the pattern to be the whole string, i.e.
^CK[0-9]{6}\.[0-9]{3}$ (Note the CK prefix).
I've also removed your leading (mismatched) parenthesis.

Related

Custom Regex format for ######-## [duplicate]

This question already has an answer here:
Learning Regular Expressions [closed]
(1 answer)
Closed 5 months ago.
I am absolutely clueless when it comes to Regex strings. I am trying to create a custom validator on a model using [RegularExpression("myValidator")] How can I create a regex expression to validate the following formats
######-##
######-#
where # is a number. Could someone help me out?
Thanks!
\d means digit.
{N} means previous symbol repeated N times
so, basically you want:
\d{6}-\d{2}
which would match 6 digits, a dash, and 2 more digits.
You can also do:
\d{6}-\d{1,2}
which would match 6 digits, a dash, and then 1 or 2 more digits, and therefore work for either format you described.

C# Regular Expression ends in _DXX where X is number [duplicate]

This question already has answers here:
How do I match an entire string with a regex?
(8 answers)
Closed 4 years ago.
I am trying to create a regular expression pattern in C# which allow you to have
next pattern: _DXX at the end of your .
Example :
04R5714A_D15 is correct
04R5714A_D05 is incorrect
04R5714A_D5 is correct
I tried : .*_D([1-9]{1}[0-9]?) but it didn't work :
.*_D[1-9]\d?$ should work for you.
Demo
.* catches everything up until your underscore
_D is a literal match
[1-9] matches one number in that range
\d? matches 0 or 1 single number (0-9)
$ is the end of the string

Check if a string only contains the characters > or < or - [duplicate]

This question already has answers here:
.NET Regex Error: [x-y] range in reverse order
(3 answers)
How to match hyphens with Regular Expression?
(6 answers)
Closed 4 years ago.
What I need is to check if a string only contains the characters > or < or -.
So I thought using a RegEx for this, and I found an SO question with the exact same problem, and it has an answer (not the accepted one but the answer with regex)
This is the SO question : String contains only a given set of characters
So I modified the expression in this question to fit my needs like this :
static readonly Regex Validator = new Regex(#"^[><- ]+$");
and I call it like this ;
Validator.IsMatch(testValue)
But it's throwing the error
x-y range in reverse order
There are lots of question on SO about this error but I cant find or understand the answer I need.
So what am I doing wrong with this RegEx?
^[-<>]+$ "-" must come first in C# regex
Escape - within character groups. ([0-9] means "zero to nine" and not "zero, dash or nine")

Regex - Minimum 6 chars and no whitespace. Everything else allowed [duplicate]

This question already has answers here:
Regular expression for not allowing spaces in the input field
(5 answers)
Closed 3 years ago.
I'm trying to use regex which checks only two things
Minimum 10 characters (No Max)
No whitespace allowed
I'm able to check minimum 10 chars with #"^[a-zA-Z0-9]{10,}$" and disallow white space with ^[^0-9 ]+$
Now the problem is, how to combine both of these and allow everything(alphanumeric including special characters) except white space
You could try to use a simpler regex pattern just to accept anything that is not a white-space: ^\S{10,}$
\S - matches any non-white-space character. More details here: https://learn.microsoft.com/en-us/dotnet/standard/base-types/regular-expression-language-quick-reference

Matching a regular expression without capturing the whole thing [duplicate]

This question already has answers here:
Capturing parts of string using regular expression in R
(3 answers)
Closed 5 years ago.
I need to parse a text file for certain information. I am using a regular expression to do so. My question is, is it possible to match an expression but only capture a relevant part, negating the need to strip the unnecessary characters after capture?
Of course, google "regex capturing groups" or check this link: https://msdn.microsoft.com/en-us/library/bs2twtah(v=vs.110).aspx#named_matched_subexpression

Categories

Resources