Match float number at the beginning of a text -Regular Expression - c#

I want to match these examples flowing a format as #.###,##
Valid examples
455,80SomeText
1,00
30,82
7,78 SomeText
622,21
8.542,85
Invalid examples
455,482
54,1
7454,50
I have tried this: ^[0-9]+(\,[0-9][0-9])
Update 1
Number format #.###,##
Could contain some text after the number

You aren't accounting for the thousands separator at all in your regex...
^[0-9]{0,3}(?:\.[0-9]{3})*,[0-9]{2}(?![0-9])
regex101 demo
If you don't want to accept ,42 either, use:
^[0-9]{1,3}(?:\.[0-9]{3})*,[0-9]{2}(?![0-9])
regex101 demo
(?:\.[0-9]{3})* allows for the thousands.
The comma doesn't need to be escaped, and (?![0-9]) (a negative lookahead) is to prevent the number from being followed by more numbers.

Try this regular expression:
^\-?\d{1,3}(\.\d\d\d)*(,\d+)?
Broken out:
^ # drop anchor at the start of the line. Then...
\-? # match an optional negative sign, followed by...
\d{1,3} # match 1-3 decimal digits, followed by...
( # a group, consisting of
\. # * a thousands separator, followed by
\d\d\d # * 3 decimal digits
)* # with the group repeated zero or more times, followed by...
( # a group, consisting of
, # * a decimal point, followed by
\d+ # * 1 or more decimal digits
)? # with the group being optional
You should note that the thousands separator and decimal point are culture-specific. Further, not all cultures clump digits in groups of 3.
To make this portable across cultures, you'll need to instantiate a suitable System.Globalization.CultureInfo, drill down to its NumberFormatInfo property and build the regular expression on the fly using the culture's rules for how numbers are composed.

I am not exactly sure what format you are looking for, but it sounds like you could use the repeat qualifiers in RegEx like ^[0-9]+(,[0-9]{2}) I think the problem in your expression is you are escaping the comma which is not a regex special character.
Here is a good reference on RegEx: http://www.regular-expressions.info/repeat.html

Related

Regex pattern infinite number of times except last one different

I'm trying to build a regex to check if a text input is valid.
The pattern is [NumberBetween1And999]['x'][NumberBetween1And999][','][White space Optional] repeated infinite times.
I need this to make an order from a string: the first number is the product id and the second number is the quantity for the product.
Examples: of good texts:
1x1
2x1,3x1
1x3, 4x1
Should not catch:
1x1,
1,1, 1x1,
9999x1
1x1,99999x1
I'm blocked there: ^(([1-9][0-9]{0,2})x([1-9][0-9]{0,2}),)*$
Thanks for helping me
You can use
^[1-9][0-9]{0,2}x[1-9][0-9]{0,2}(?:,\s*[1-9][0-9]{0,2}x[1-9][0-9]{0,2})*$
The pattern matches:
^ Start of string
[1-9][0-9]{0,2}x[1-9][0-9]{0,2} Match a digit 1-9 and 2 optional digits 0-9, then x and again the digits part
(?: Non capture group to repeat as a whole
,\s* Match a comma and optional whitespace char
[1-9][0-9]{0,2}x[1-9][0-9]{0,2} Match the same pattern as at the beginning
)* Close the non capture group and optionally repeat it to also match a single part without a comma
$ End of string
Regex demo

I cant create a regular expression for the spanish number format

I am trying to transform this regex regular expression on c# to the spanish number format.
I want it to accept numbers with this format
Decimals with a comma and the thousands separator with a dot
10.000.000,262 <br>200,262<br>1.000.000
RegEx:
^\$?(\d{1,3},?(\d{3},?)*\d{3}(.\d{0,3})?|\d{1,3}(.\d{10})?)
To match decimals with a comma and the thousands separator with a dot you could use:
\b\d{1,3}(\.\d{3})*(,\d+)?\b
Explanation
\b Word boundary
\d{1,3} Match a digit 1 - 3 times
(\.\d{3})* match a dot and a 3 digits and repeat that zero or more times
(,\d+)? Match a comma and one or more digits and make that optional
\b Word boundary
Regex demo
Note that if you want to match a dot literally you should escape it \.
If you want to use a 1 to 3 number rule, it would be something like this
(?<![\d.,])(?:\d{1,3}(?:\.\d{3})*(?:,\d{1,3})?)(?![\d.,])
Readable version
(?<! [\d.,] )
(?:
\d{1,3}
(?:
\.
\d{3}
)*
(?: , \d{1,3} )?
)
(?! [\d.,] )
It seems you are after a very specific input format with a fixed number of allowed fraction digits. I think the pattern you want looks like this:
(\d{1,3}.?(\d{3}.?)*\d{3}(,\d{0,3})?|\d{1,3}(,\d{10})?)
Demo

Regular Expression for decimal numbers (at max 3 decimal numbers with comma)

How can I write a regular expression that validates an input text box that should contain only decimal values? The value can have at max 3 decimals (but also none) with comma as the separator.
For example, these values given below are valid:-
1,234
1,23
1,2
1
These are not valid:
1,2345 (too many decimal numbers)
A (a letter is not a number)
(a space or string empty)
1.234 (used a dot instead of a comma)
Try something like this:
\d+(?:,\d{1,3})?
Explained:-
\d+ # multiple digits
(?: # start non-capturing group
, # a comma
\d{1,3} # 1-3 digits
)? # end non-capturing group, made optional
You could use a pattern like this:
[0-9]+(,[0-9]{1,3})?
How about #"\d+,?\d{0,3}": 1 or more digits, then an optional comma, then 0 to 3 more digits. This assumes that you allow any number of digits before the comma. In your examples you only have one, in which case you would want to remove the +.
If the value 1, is not valid, you'll have to move the ? to the end: #"\d+(,\d{1,3})?"
Note if you only want one character before the decimal, remove the +
^\d+(,(\d?){3})?$
^ //start
\d+ //one or more decimal digits
(,(\d?){3})? //a comma, followed by up to 3 decimal digits, optionally
$ //end
If you don't want 1, to be accepted, then the middle section can be (,\d(\d?){2})?

Regular Expression: Section names with unknown length?

I have a text block that is formatted like this:
1.2.3.4.5 or 1.2222.3.4.5 or 1 or 1.2 etc
An unknow number of numbers and dots (sections of a legal document)
How can I capture the full section (1.2.3.4.5) into a group?
I use C# but any regex is fine, aI can translate it.
UPDATED
Use this Regex:
Regex.Matches(inputString, #"\d[\.\d]*(?<!\.)");
explain:
\d digits (0-9)
[.\d]* any character of: '.', digits (0-9)
(0 or more times, matching the most amount possible))
(?<! subexpression) Zero-width negative lookbehind assertion.
string s = "1.2.3.4.5 or 1.2222.3.4.5 or 1 or 1.2 or 2222.3333.111.5 etc";
var matches = Regex.Matches(s, #"\d+(\.\d+)*").Cast<Match>()
.Select(m => m.Value)
.ToArray();
well, if you know you can't go beyond 5, then you can do
#"1+((.2+)((.3+)((.4+)(.5+)?)?)?)?"
and you can expand on that pattern for every symbol, up to a finite number of symbols
the + means any number of occurrences of the symbol, but at least 1. IF 0 is valid, you can use * instead
put ?: after an opening parenthesies if you don't want the pattern to be captured
like example: (?:abc)
I ommitted them to make the regex more readable.
the ? after the parenthesies, means 1 or 0 of the preceding symbol.
Now if you don't know how far you string can go, for instance
"1.2.3.4......252525.262626.272727.......n.n.n" than my intuition tells me that you can't do that with regex.

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