Ignore date in a string with numbers using regular expression - c#

I have a little Problem.
i use [0-9\,.]*
to finde a decimal in a string.
And ([^\s]+) to find the text behind the first number.
The string looks normally like this. 1 number a text and than a date:
1.023,45 stück
24.05.10
but sometimes I had just the date and then i become 240510 as decimal.
And sometimes I had just the decimal.
How should I modify the regex to find the date if existing and remove it?
And then look for a decimal an select this if existing.
Thanks in advance.

Divide and conquer
Check for the date first and remove the match from the string
([0-9]{1,2}\.){2}[0-9]{1,2}
Find the number using your original regex
[0-9\,.]*
If you need it find the unit of quantity (assuming that you will only have it as lower case with u Umlaut)
([a-zü]+)
See http://regexe.de/ (German) and http://www.regexr.com/ (English) for some useful information and tools for dealing with regex.

I suggest matching the number in a more restricted way (1-3 digits, then . + 3 digits groups if any, and a decimal separator with digits, optional).
(?s)(?<number>\d{1,3}(?:\.\d{3})*(?:,\d+)?)\s+(.*?)(?:$|\n|(?<date>\d{2}\.?`\d{2}\.?(?:\d{4}|\d{2})))
See demo
The number will be held in ${number}, and the date in ${date}. If the string starts with something very similar to a date (6 or 8 digits with optional periods), it won't be captured. If the date format is known (say, the periods are always present), remove the ?s from \.?s.
(?s) at the beginning will force the period . to match a new line (maybe it is not necessary).

Related

Regular expression with only numbers, dots and minimum value

Im trying to make a regular expression that accept only numbers, dots and that has minimum value and max value.
E.g:
1.000 - valid
100.000 - valid
100.000a -not valid
.10 - not valid
100 - not valid
I have this, which works as i want with numbers and dots, only one thing is missing here, and that is minimum and maximum validation.
#"^([+-]?\d{1,3}(?:,\d{1,3})*(?:\.\d+)*)$"
PS:
Im using data annotations on .net core
Update
I have a javascript that separates users input in input field to thousand format like:
From: 1000000
To: 1.000.000
For better user experience.
But, the problem is the validation with data annotations.
With the RegularExpression i have above is working with the dots, i just need a minimum and maximum value.
I have tried with Range(min, max), but it recognise it as an invalid input because of the dots.
Regards
Given your comment I assume this is what your after:
^[+-]?[1-9]\d{0,2}(?:\.\d{3})+$
It matches a range of 1-3 digits (the first not being a 0), then followed by at least one (no upper limit) sequence of . + three digits.
This will match any number larger than one thousand with . as thousand separators. And since your attempt allowed it, this also allows an optional sign in the beginning.
See it in action here at regexstorm. Note! I can't get $ and multiline to work there so a \s is used instead to illustrate.
Considering that after every number there is space character in the string
This should do the job :
regex = #"[0-9]{1,3}\.[0-9]{1,5}"
Update :
As the obtained text will be numbers , so you could simply validate them for a range check .
For reference please have a look :regex reference
Let me know , if it doesn't works

Validating Positive number with comma and period

I need a regular expression validation expression that will
ALLOW
positive number(0-9)
, and .
DISALLOW
letter(a-z)
any other letter or symbol except . and ,
for example, on my asp.net text box, if I type anything#!#--, the regular expression validation will disallow it, if I type 10.000,50 or 10,000.50 it should allowed.
I've been trying to use this regex:
^\d+(\.\d\d)?$
but my textbox also must allow , symbol and I tried using only integer regex validation, it did disallow if I type string, but it also disallow . and , symbol while it should allow number(0-9) and also . and , symbol
Don't Use \d to match [0-9] in .NET
First off, in .NET, \d will match any digits in any script, such as:
654۳۲١८৮੪૯୫୬१७੩௮௫౫೮൬൪๘໒໕២៧៦᠖
So you really want to be using [0-9]
Incomplete Spec
You say you want to only allow "digits, commas and periods", but I don't think that's the whole spec. That would be ^[0-9,.]+$, and that would match
...,,,
See demo.
Tweaking the Spec
It's hard to guess what you really want to allow: would 10,1,1,1 be acceptable?
We could start with something like this, to get some fairly well-formed strings:
^(?:[0-9]+(?:[.,][0-9]+)?|[1-9][0-9]{0,2}(?:(?:\.[0-9]{3})*|(?:,[0-9]{3})*)(?:\.[0-9]+)?)$
Play with the demo, see what should and shouldn't match... When you are sure about the final spec, we can tweak the regex.
Sample Matches:
0
12
12.123
12,12
12,123,123
12,123,123.12456
12.125.457.22
Sample Non-Matches:
12,
123.
1,1,1,1
Your regex would be,
(?:\d|[,\.])+
OR
^(?:\d|[,\.])+$
It matches one or more numbers or , or . one or more times.
DEMO
Maybe you can use this one (starts with digit, ends with digit):
(\d+[\,\.])*\d+
If you need more sophisticated price Regex you should use:
(?:(?:[1-9]\d?\d?([ \,\.]?\d{3})*)|0)(?:[\.\,]\d+)?
Edit: To make it more reliable (and dont get 00.50) you can add starting and ending symbol check:
(^|\s)(?:(?:[1-9]\d?\d?([ \,\.]?\d{3})*)|0)(?:[\.\,]\d+)($|\s)?
I think the best regex for your condition will be :
^[\d]+(?:,\d+)*(?:\.\d+)?$
this will validate whatever you like
and at the same time:
not validate:
numbers ending in ,
numbers ending in .
numbers having . before comma
numbers having more than one decimal points
check out the demo here : http://regex101.com/r/zI0mJ4
Your format is a bit strange as it is not a standard format.
My first thought was to put a float instead of a string and put a Range validation attribute to avoid negative number.
But because of formatting, not sure it would work.
Another way is the regex, of course.
The one you propose means :
"some numbers then possibly a group formed by a dot and two numbers exactly".
This is not what you exepected.
Strictly fitted your example of a number lower than 100,000.99 one regex could be :
^[0-9]{1-2}[\.,][0-9]{3}([\.,][0-9]{1-2})?$
A more global regex, that accept all positive numbers is the one posted by Avinash Raj : (?:\d|[,\.])+

How to set a regular expression for amount

I have a textbox and in it a value like $8.00 I want to validate this textbox to always check for amount values and not accept letters or anything other than a value in the format of 0.00. How can I achieve this in a RegularExpressionValidator?
Thank you for the help.
The RegEx you are looking for is #"^\d+\.\d\d"
It matches strings with 1+ digits before point and exactly two digits after
If you want it to allow start a string from $, then use #"^\$\d+\.\d\d" or #"^\$?\d+\.\d\d" for optional $.
If you want $ to be separated from digits with spaces then use #"^\$?\s*\d+\.\d\d"
The following regular expressiong will allow numbers in the following format (12345.67, 0, 0.1)
^\d{1,5}(.\d{1,2})?$
I used one of the following before i hope it helps try it.
\d{1,3}(.)\d{1,2}
or
\d{1,3}.\d{2}

.Net Regex for mm-dd-yy and others + yyymmdd

I feel like I am chasing my tail.
I am trying to arrive at a .Net regex that will match on the following:
mm-dd-yy
m-dd-yy
mm-d-yy
m-d-yy
and (no dashes)
yyyymmdd
One or two digits followed by a dash, followed by one or two digits, followed by a dash, follows by to digits or eight digits:
(\d{1,2}-\d{1,2}-\d{2})|(\d{8})
A very simple RegEx that just matches to the digits being in the correct places and matches all your formats:
^[0-9]{1,2}-[0-9]{1,2}-[0-9]{2}$|^[0-9]{8}$
This does not validate those dates as actual possible dates, to do this you would be better off using DateTime.TryParse
Do you need to use Regex, or do you just care that it is a valid date?
DateTime result;
if(DateTime.TryParse(input, out result))
{
// you have your date in result
}
Well it all comes down to how strict you want the matching to be.
[0-9]+\-[0-9]+\-[0-9]+
will match all the four top ones. But you can enter "99-99-99".
You can be a bit more strict with:
([0-1][0-2]|[0-9])\-(3[0-1]|[0-2]?[0-9]+)\-[0-9]{1,2}
This will only match dates where each component is in a valid range, but only to an extent. It would still match a February with 31 days (which it won't have in the Gregorian calendar). Also it will match 13-04-18 from 3 and onwards. You can use anchors to make it match the whole text (add ^ at the beginning and $ at the end of the regex), but then it won't be able to find the dates inside a text.
You can add a precondition to make sure there's no weird digits around it though. Negative look-behind and negative look-ahead.
(?<![0-9])([0-1][0-2]|[0-9])\-(3[0-1]|[0-2]?[0-9]+)\-[0-9]{1,2}(?![0-9])
And so forth, but this regex has already become a proper behemoth. I would go with the second or third version and use DateTime.Parse to validate it.
There's only so far you can go with regex for dates before they become write-once and insane :) (what about leap years for example, etc. etc.)

How can I display a negative symbol in .NET?

I want to display a negative symbol from a string in .NET. I want a string that represents an equation that looks something like this:
7--5=12
But when displayed, I want the 2nd minus sign to be slightly raised so it looks more natural as a negative sign instead of just 2 minus signs in a row.
Is this possible?
Not sure if theres a character for what you want but a simple solution (and one that would be easily understood and implemented) would be to surround your negative number in brackets:
7 - (-5) = 13
Use the Unicode character SUPERSCRIPT MINUS (U+207B) ⁻.
For example:
7-⁻5 = 13
EDIT: Or, with a MINUS SIGN (U+2212) ⁻ for the minus:
7 − ⁻5 = 13
Provided that you're using unicode you can use a true minus sign, "−" (U+2212) rather than a hyphen-minus, "-" (U+002D). Just be aware that it's not ASCII compatible
Here's your example showing them:
7 - −5=13
Also, here's some fun wiki-articles on all sorts of dash-hyphen-minus lines:
http://en.wikipedia.org/wiki/Dash#Common_dashes
http://en.wikipedia.org/wiki/Minus_sign#Character_codes
This is a great resource on format strings in C#:
SteveX Compiled - Format Strings
You can choose how a negative number is displayed by using a range expression for your format string. It's in the format:
{0:<PositiveFormat>;<NegativeFormat>;<ZeroFormat>}
For example, this is how to display a negative number in parenthesis and the word "Zero" for 0:
{0:#;(#);Zero}
Using this technique, I think you can try it with the superscript version of negative (which is ascii code U+207B) in the negative format string.
{0:#;⁻#;Zero}
HTH, Anderson
Traditionally in math typography you use an en dash U+2013 or minus U+2212 (but not a hyphen!) for both binary (subtraction) and unary (negation) minus, and they are differentiated with spacing (spaces before and after a binary minus, no space between a unary minus and the number it negates).
But if you want to further distinguish the unary, I'd recommend substituting the superscript minus U+207B (but keep the spacing around the subtraction minus):
7 − ⁻5 = 13
You can use the Unicode character U+2212 (Minus Sign): 7-−5=13
In the font I'm using, the minus sign is displayed slightly raised relative to the dash. Your results may vary.
Unicode "superscript minus" http://www.fileformat.info/info/unicode/char/207b/index.htm
char c = '\u207b';

Categories

Resources