Error handling to check if float is .02f - if-statement

Hi in my settings for an app the user must input a number that has 2 decimal digits no more or no less. So values such as 125.22, 11.09, 63.88 are acceptable values such as 1.1, 23.1823, 12, 293.0001 are not. How can I make an if statement checking if these floats values are inputted correctly?

Regular expressions can come handy in this situation. You can convert the number to string and match against:
[0-9]+\.[0-9][0-9]
That is:
[0-9] : Match a character, which is inside the group 0--9 -> 0123456789
+ : Between 1+ times, as much as possible
\. : Match a dot character
[0-9] : Match a character, which is inside the group 0--9 -> 0123456789
[0-9] : Match a character, which is inside the group 0--9 -> 0123456789
Honestly, my experience in Objective-C (and Macs) is NULL, but you can have a look here on how to create and test regular expressions objects against strings.

Related

Regular Expression Stopping at Specified Value

I have to use a regular expression to parse values out of a swift message and there are some situations where the behaviour is not what I want.
Lets say I am after something with a particular pattern - in this case a BIC (6 letters, followed by 2 letters or digits followed by optional XXX or 3 digits)
([A-Z]{6}[A-Z0-9]{2}[XXX0-9]{0,3})
this is fine but now I want to look for these bank codes in particular fields. In swift a field is denoted with : and has some numbers and sometimes a letter.
so I want to match a BIC value in field 52A
I can do the following
(52A:[A-Z]{6}[A-Z0-9]{2}[XXX0-9]{0,3})
which would match 52A:AAAAAAAAXXX
my problem is you can have things before and after this value - and the value itself might not exist in the field you want
so I can wildcard the reg ex to allow for things before it for example
(52A:.*?[A-Z]{6}[A-Z0-9]{2}[XXX0-9]{0,3})
matches 52A:somerubbishAAAAAAAAXXX
but if there isnt something within this field - the reg ex continues to search for the pattern and this is where i have a problem.
for example the above reg ex matches this 52A:somerubbish:57D:AAAAAAAAXXX
Question
I need the reg ex to stop on the first field that is after it (it might not always be 57D but it will always follow the format [0-9]{2}[A-Z]{0,1})
so the above example shouldnt return a match as the pattern I am after is not contained in the 52A section
Does anyone know how I can do this?
Change .*? to [^:]*?:
(52A:[^:]*?[A-Z]{6}[A-Z0-9]{2}[XXX0-9]{0,3})
[^:] means "any character except :", which ensures the match doesn't run into the next field.
See live demo.
Also, unless your situation requires you to match your target as group 1, you don't need the outer brackets: the entire match (ie group 0) will be your target.
I suspect instead of [XXX0-9]{0,3} you want (XXX|\d{3})? (XXX or 3 digits, but optionally) or perhaps (XXX|\d{1,3})? (XXX or up to 3 digits, but optionally)
Using [XXX0-9]{0,3} (which is the same as [X0-9]{0,3}) is a character class notation, repeating 0-3 times an X char or a digit.
If the value itself can also contain a colon, you can match any character as "rubbish" as long as what is directly to the right is not the field format.
52A:(?:(?![0-9]{2}[A-Z]?:).)*[A-Z]{6}[A-Z0-9]{2}(?:[0-9]{3}|XXX)?
The pattern matches:
52A: Match literally
(?:(?![0-9]{2}[A-Z]?:).)* Match any character asserting not 2 digits, optional char A-Z and : directly to the right
[A-Z]{6}[A-Z0-9]{2} Match 6 chars A-Z and 2 chars A-Z or 0-9
(?:[0-9]{3}|XXX)? Optionally match 3 digits or XXX
See a regex demo.

Match all non-numeric characters between two underscores

I am using a regular expression to extract all non-numeric characters between two underscores from a string.
JohnDoe_King234_sample
I need the following output from the string: King
I have tried the following regular expression: (?<=_).\D*(?=_)
(Look positively forward for _ then match non numeric characters then look positively behind _ )
If my string is:
JohnDoe_King_sample
then my expression returns King. If my string is:
JohnDoe_King234_sample
then my expression does not match.
(?<=_).\D*(?=_)
Expected results: King
Actual results:
You may use
(?<=_)[^_\d]+(?=\d*_)
See the regex demo
Details
(?<=_) - a _ should be right before the current location
[^_\d]+ - any 1 or more chars other than _ and digits -
(?=\d*_) - there must be 0 or more digits followed with one _ immediately to the right of the current location.
NOTE: In case you may have digits anywhere inside that substring between underscores, if you have a way to process the string with some programming language, you might consider a _([^_]+)_ regex to extract the first match, then grab Group 1 value and remove all digits from it using a simple \d+ pattern with a regex replace method/function.

MVC Regex model validation

I want to have at least 3 numbers in my password.
This validation always fails :
[RegularExpression(#"[\d]{3}", ErrorMessage ="Password must have at least 3 digits")]
[Display(Name = "NewPassword", ResourceType = typeof(ModelResources))]
public string NewPassword { get; set; }
I have also tried with () in the expression. What am I doing wrong?
To require at least three digits, you may use
"^([^0-9]*[0-9]){3}.*$"
or
#"^(?:\D*\d){3}.*$"
or the least effecient regex:
^(.*\d){3}.*$
See regex demo
Note that in .NET \d matches more than just [0-9] digits, it can match Arabic and other digits. The [^0-9] and \D matches any character but a digit.
^ - start of string
([^0-9]*[0-9]){3} - exactly 3 occurrences of 0 or more sequences of
[^0-9]* (or \D*) 0 or more characters other than a digit
[0-9] (or \d) - a digit
.* - 0 or more characters other than a newline
$ - end of string
Please note that using opposite character classes to match 3 digits follows the principle of contrast, which is very effecient compared to dot matching as it involves much less backtracking. You can check for yourself at regex101.com at regex debugger section: it takes 2 backtracking steps for my regex to complete matching and it takes ^(.*\d){3}.*$ ~180 steps to complete matching in the same string.

Regex to find integers and decimals in string

I have a string like:
$str1 = "12 ounces";
$str2 = "1.5 ounces chopped;
I'd like to get the amount from the string whether it is a decimal or not (12 or 1.5), and then grab the immediately preceding measurement (ounces).
I was able to use a pretty rudimentary regex to grab the measurement, but getting the decimal/integer has been giving me problems.
Thanks for your help!
If you just want to grab the data, you can just use a loose regex:
([\d.]+)\s+(\S+)
([\d.]+): [\d.]+ will match a sequence of strictly digits and . (it means 4.5.6 or .... will match, but those cases are not common, and this is just for grabbing data), and the parentheses signify that we will capture the matched text. The . here is inside character class [], so no need for escaping.
Followed by arbitrary spaces \s+ and maximum sequence (due to greedy quantifier) of non-space character \S+ (non-space really is non-space: it will match almost everything in Unicode, except for space, tab, new line, carriage return characters).
You can get the number in the first capturing group, and the unit in the 2nd capturing group.
You can be a bit stricter on the number:
(\d+(?:\.\d*)?|\.\d+)\s+(\S+)
The only change is (\d+(?:\.\d*)?|\.\d+), so I will only explain this part. This is a bit stricter, but whether stricter is better depending on the input domain and your requirement. It will match integer 34, number with decimal part 3.40000 and allow .5 and 34. cases to pass. It will reject number with excessive ., or only contain a .. The | acts as OR which separate 2 different pattern: \.\d+ and \d+(?:\.\d*)?.
\d+(?:\.\d*)?: This will match and (implicitly) assert at least one digit in integer part, followed by optional . (which needs to be escaped with \ since . means any character) and fractional part (which can be 0 or more digits). The optionality is indicated by ? at the end. () can be used for grouping and capturing - but if capturing is not needed, then (?:) can be used to disable capturing (save memory).
\.\d+: This will match for the case such as .78. It matches . followed by at least one (signified by +) digit.
This is not a good solution if you want to make sure you get something meaningful out of the input string. You need to define all expected units before you can write a regex that only captures valid data.
use this regular expression \b\d+([\.,]\d+)?
To get integers and decimals that either use a comma or a dot plus the next word, use the following regex:
/\d+([\.,]\d+)?\s\S+/

A regular expression that matches 25 chars and starts with digits

I have a text field which I need to validate using a regex. My requirement is as follow:
CCCCNNNNNN or CCCCNNNNNNN (Template)
1234ABCDEFG or 123-ABCDEFG (Example string)
Rules:
The whole string is maximum 25 characters
The first four characters (CCCC) must be alphanumeric
CCCC is 4 characters exactly and can be digits or number
CCCC can have a dash sign as 4th character
NNNNNNNNNNNN can be up to 21 characters and only numbers
E.g. AAAA 1234 A58- is a valid string for CCCC.
Here is my research notes:
I will need to match numerics first
I will need the + char to specify to match this pattern X times
I will need to match letters after that for 8-9 spaces
There is a wonderful post on RegEx patterns here:
Matching numbers with regular expressions — only digits and commas
My goal is to apply this REGEX pattern to a text box Mask in a WinForms app.
....
....
...yeah - I think the answer you are looking for (and I stress "think") is this expression:
^[0-9A-Za-z]{3}[0-9A-Za-z-]\d{0,21}$
thats:
^ # assert beginning (not in the middle)
[0-9A-Za-z]{3} # three characters that are: 0-9 or a-z (upper or lower)
[0-9A-Za-z-] # one character that is: 0-9 or a-z (upper or lower) or a dash
\d{0,21} # anywhere from 0 to 21 digits
$ # assert at the end (not somewhere in the middle
If you want to match several cases of this expression, put the above expression (minus the assertions) into parantheses (()) along with whatever is allowed to separate these values - I chose \s or "whitespace") and then use the + quantifier:
^([0-9A-Za-z]{3}[0-9A-Za-z-]\d{0,21}\s+)+$
will match/validate the following input:
1234567890 AAAA123456789012345678901 GGG-123 hhh5 A1B2000000000
If you wanted something else, you'll have to ask a clearer question (there's a lot of contradiction and repetition in your question that makes it EXTREMELY confusing)