Regular expression for decimal numbers with limiting character size - c#

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

Related

Regex pattern to match numbers in certain conditions

First time posting, please forgive the formatting. Not really a programmer, I work in C# with the Revit and AutoCAD APi's. Important to note, as the Revit API is a bit of mess, so the same code may produce different results in a different API. So I have three basic string patterns where I want to return certain numbers depending on what their prefix & suffix. They could be surrounded by other text than what I show, and the actual numbers and positions within the string may vary.
String 1: (12) #4x2'-0 # 6 EF
String 2: (12) #4 # 6 EF
String 3: STAGGER 2'-0, SPCG AT 6 AT 12 SLAB
The code I'm using:
if (LengthAsString.IsMatch(remarkdata) == true)
{
Regex remarklength = new Regex(#"isnertRegexPatternhere");
if (remarklength.IsMatch(remarkdata))
{
remarkdata = remarklength.Replace(remarkdata, "${0}\u0022");
}
}
remarkdata is the strings from above, and im adding inch marks " after each number match.
The patterns ive tested and their returns:
String 1 String 2 String 3
\d+(?!['-]|([(\d+)])) 0,6 4,6 0,6,12
(?<![#])\d+ 12,2,0,6 12,6 2,9,6,12
\d+(?= #)|(?<=# )\d+ 0,6 6 no matches
expected results: 0,6 6 0,6,12
so im close, but no cigar. Thoughts?
Double Edit: looking for the numbers that aren't preceded by #, nor between (). Ignore # and x, they may or may not be there.
You seem to be looking for
(?<!#)\d+(?!.*(?:['-]|[#x]\d))
See the regex demo
Details
(?<!#) - a negative lookbehind that fails the match if there is a # immediately to the left of the current location
\d+ - 1 or more digits (or [0-9]+ to only match ASCII digits)
(?!.*(?:['-]|[#x]\d)) - a negative lookahead that fails the match once there are any 0+ chars other than newline (.*) followed with ', -, or #/x followed with a digit immediately to the right of the current location.
Note that in case your strings always have balanced non-nested parentheses, and you may have (123) substrings after # or x1, you may also want to add [^()]*\) into the lookahead
(?<!#)\d+(?!.*(?:['-]|[#x]\d)|[^()]*\))
to avoid matching digits inside the parentheses.
See another .NET demo.

Regex failing in max length

I want regex which will allow following format
1234567-8
123456B
Now here if user enter second pattern then he should be lock to enter maximum 7 characters so
1234568B
123456V1
this becomes invalid
I have tried
[0-9]{7}-[0-9]|[[0-9]{6}[A-z]{1}]{7,7}
but this fails
For the sample input you provided, you may use ^([0-9]{7}-[0-9]|[0-9]{6}[A-Za-z])$.
A bit more contracted version: ^[0-9]{6}(?:[0-9]-[0-9]|[A-Za-z])$.
Note that 1234567-8 has 7 digits and a hyphen followed with a digit, so the whole string length cannot be limited to just 7 characters all in all.
In .NET and almost all other regex flavors [A-z] is a mistake, as it can match more than just letters.
Placing a quantifier {1} into a character class makes it a simple symbol combination, so [{1}] matches either { or 1 or }.
The {7,7} (={7}) will not limit the whole string length to 7, as you do not have anchors (^ and $) around the expression and you "ruined" the preceding quantifiers by putting them into a character class.
I think this is what you need:
^(\d{7}-\d|\d{6}[A-Z])$
7 digits, dash, digit OR 6 digits, 1 large latin letter.
^\d{6}(?:\d-\d|[A-Z])$
It can satisfy well with 2 your above formats
1234567-8
123456B

Using Regular Expression to find exact length match multiple times

I need a regular expression to find groups of exactly 8 numbers in a row. The closest I have gotten is:
[0-9]{8}
but it's not exactly what I need. If I had a number that was 9 long it will match the first 8 but I want it to ignore it if it's longer or shorter than 8.
Here are some examples
1234567890 <- no match, it's longer than 8
12345678 <- match: "12345678"
1234567809876543 <- match 1: "12345678", match 2: "09876543" (two groups of 8)
,,111-11-1234,12345678, <- match: "12345678"
To summarize, for every group of exactly 8 numbers make a match.
I'm working with some results of OCR (Optical Character Recognition) and I have to work with the shortcomings of the results so my input can be varied as in the examples above.
Here is some use case data: http://pastebin.com/uijF9K9n
You can use the following regex in .NET:
(?<=^|\D|(?:\d{8})+)\d{8}(?=$|\D|(?:\d{8})+)
See regex demo
It is based on variable-width lookbehind and a lookahead.
Regex breakdown:
(?<=^|\D|(?:\d{8})+) - only if at the string start (^), or preceded with not a digit (\D) or 1 or more sequences of 8 digits ((?:\d{8})+)...
\d{8} - match 8 digits that are followed by...
(?=$|\D|(?:\d{8})+) - either end of string ($) or not a digit (\D) or 1 or more sequences of 8 digits ((?:\d{8})+).
IMPORTANT:
If I got a downvote for the "extra" complexity compared with another answer, note our solutions are different: my regex matches 8-digit number in ID12345678, and the other one does not due to the word boundary.
You can also try this regex
(?:\b|\G)\d{8}(?=(?:\d{8})*\b)
(?:\b|\G) \b match a word boundary | or \G continue where last match attempt ended
\d{8} matches 8 digits [0-9] followed by a lookahead (?=... to check
(?:\d{8})*\b if followed by any amount of {8 digits} until another word boundary
It will match {8 digits} or out of a sequence of such if between two word boundaries.
See demo at regexstorm
\b[0-9]{8}\b this will give you what you want
For more details check this out
http://www.rexegg.com/regex-boundaries.html

c# Conditional Regular Expression String Match

I am trying to use c# Regular Expression to match a particular string of characters but I can not figure out how to do it. Any help is appreciated.
The string that I am trying to match is as follows, where A is an uppercase alpha character, X is an upper case alpha-numeric character and # is 0, 1 or 2.
AA-#-XX-X-XXX-XXXXXXX-XXXXXXXX
So any of the following would match the string above.
XY-1
MM-0-AB
MM-0-AB-1-ABC-1234567
VV-2-XX-7-CCC-ABCDEFG-12345678
Any any of the following would NOT match.
QQ-7-AA (Only 0, 1, 2 are allowed at the second level.)
QQ-2-XX-7-CC (Partial characters for that level.)
QQ-2-XX-7-CCC-ABCDEFG- (Can not end in a dash.)
QQ-2-XX-7-CCC-ABCDEFG-123456 (Partial characters for that level.)
So far (not that far really) I have as the pattern to match #"^[A-Z]{2}", but I am unsure how to match conditionally (I'm not even sure if conditionally is the proper term to use) the rest of the string, but only if it is there. Do I need to write 7 different statements for this? Seems unreasonable, but I could be wrong.
Have a look at the Regular Expression Language. You need the following elements:
uppercase alpha character: [A-Z]
upper case alpha-numeric character: [A-Z0-9]
0, 1 or 2: [0-2]
dash: -
match x exactly n times: x{n}
match x zero or one time: x?
define a subexpression: (...)
Examples:
two uppercase alpha characters: [A-Z]{2}
two uppercase alpha characters, followed by a dash: [A-Z]{2}-
two uppercase alpha characters, followed by a dash, followed by 0, 1 or 2: [A-Z]{2}-[0-2]
two uppercase alpha characters, followed by a dash, followed by 0, 1 or 2, but with the subexpression consisting of the dash and 0, 1 or 2 occurring zero or one time:
[A-Z]{2}(-[0-2])?
and so on...
Resulting expression:
^[A-Z]{2}(-[0-2](-[A-Z0-9]{2}(-[A-Z0-9](-[A-Z0-9]{3}(-[A-Z0-9]{7}(-[A-Z0-9]{8})?)?)?)?)?)?$

RegEx for limited decimal

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.

Categories

Resources