I would like to show prices in the following formats.
100
100,20
1.000,20
11.000,20
111.000,20
1.111.000,20
I have made this regex expression, \d+(\,\d+)? but it only outputs these numbers:
100
100,20
1000,20
11000,20
1111000,20
What I'm missing is the thousand-separator. How can I add this?
I have already read these articles, but still no luck.
MSDN: Regular Expression Language - Quick Reference
DevExpress: Mask Type: Extended Regular Expressions
You may use
\d{1,3}(\.\d{3})*(\R.\d+)?
Here,
\d{1,3} - matches 1 to 3 digits
(\.\d{3})* - matches 0 or more sequences of a literal . followed with exactly 3 digits
(\R.\d+)? - matches an optional (1 or 0) sequences of:
\R. - a decimal separator specified by the System.Globalization.NumberFormatInfo.NumberDecimalSeparator property of the current culture
\d+ - 1+ digits
Unfortunately, there is no digit grouping symbol pattern in DevExpress validation regex, so you might want to hard-code the decimal separator the same way as the "thousand separator" (i.e. \R. -> ,).
Related
I have a working regex that matches floating point as well as integer but I need one to only match floating point (number of decimals can be any).
This is what I have so far
Regex regex = new Regex(#"^-?(?=.*[1-9])\d+(\.\d+)?$", RegexOptions.IgnoreCase);
How do I mod it to only match the floating point?
The regex you're looking for is (I've split it into groups for the explanation):
Regex regex = new Regex(#"^(\+?)([0-9]*)(\.)([0-9]+)$");
Explanation:
Group 1 - an optional plus sign at the beginning.
Group 2 - optional digits before the dot (why optional? because, for example, .345 is a valid number - and stands for 0.345).
Group 3 - the decimal dot.
Group 4 - numbers after the dot. One comment: This regex will accept numbers such as 12345.0 although is not a really decimal. I don't see how to solve this just with regex (without code).
I think you need this one
Regex regex = new> Regex(#"^([-+]?)(([0]{1})|([1-9]+([0-9]*)))(\.)([0-9]+)$");
Group 1: Optional "-" sing on the begin.
Group 2 - 5: The number begin whit 0 OR begin whit another num (1-9)
one or more. In this Case is not posible to match (EX: 001.55 - It's
not a float number).
Group 6 - Include "."
Group 7 - Include one or more (0-9)
I have to find a decimal in the pdf, which comes under the column "charge".
So, i have come across the regular expression to find the decimal which works fine. But in one of the pdf, i have in the below format.
Pdf Text - Charge (country) Eighteen Thousand one hundred Eighty One and 75/100 18,181.75
Expected - 18,181.75
Regular expression which used to find decimal after the text "Charge": (Charge ([0-9]*)(\,?[ ]?[0-9])+(.[0-9]+))
So, i want to ignore whatever comes in mid of "charge" and the decimal. and display the decimal number. Any help?
case 2: "18,181.75" sometimes may come before "Charge" as well. Like "18,181.75 Charge some text here..."
You may make use of .NET regex unlimited-width lookbehinds:
Regex.Match(s, #"(?<=\bCharge\b.*)\d[\d,]*\.\d+|\d[\d,]*\.\d+(?=.*?\bCharge\b)")
See the regex demo
Details
(?<=\bCharge\b.*)\d[\d,]*\.\d+ - a location preceded with a Charge as a whole word with chars other than newline after it, and then matches a digit followed with 0+ commas or digits, then a dot and 1+ digits
| - or
\d[\d,]*\.\d+(?=.*?\bCharge\b) - a digit followed with 0+ commas or digits, then a dot and 1+ digits, and that should be followed by any 0+ chars other than newline as few as possible and then Charge as a whole word
Below regular expression should help you.
Charge.*[0-9]+([,]?[0-9]+)*\.([0-9]){0,2}$
Hope this works.
What about this :
(?<=[Cc]harge.)([0-9],[0-9].[0-9])|[0-9],[0-9].[0-9](?=\s[Cc]harge)
I'm very new to regex And I'm trying to use a regular expression to turn a credit card number which will be part of a conversation into something like 492900******2222
As it can come from any conversation it might contain string next to it or might have an inconsistent format, so essentially all of the below should be formatted to the example above:
hello my number is492900001111222
number is 4929000011112222ok?
4929 0000 1111 2222
4929-0000-1111-2222
It needs to be a regular expression which extracts the capture group of which I will then be able to use a MatchEvaluator to turn all digits (excluding non digits) which are not the first 6 and last 4 into a *
I've seen many examples here on stack overflow for PHP and JS but none which helps me resolve this issue.
Any guidance will be appreciated
UPDATE
I need to expand upon an existing implementation which uses MatchEvaluator to mask each character that is not the first 6 or last 4 and ideally I dont want to change the MatchEvaluator and just make the masking flexible based on the regular expression, see this for an example https://dotnetfiddle.net/J2LCo0
UPDATE 2
#Matt.G and #CAustin answers do resolve what I asked for but I am hitting another barrier where I cant have it be so strict. The final captured group needs to only take into account the digits and as such maintain the format of the input text.
So for example:
If some types in my card number is 99 9988 8877776666 the output from the evaluation should be 99 9988 ******666666
OR
my card number is 9999-8888-7777-6666 it should output 9999-88**-****-6666.
Is this possible?
Changed the list to include items that are in my unit tests https://dotnetfiddle.net/tU6mxQ
Try Regex: (?<=\d{4}\d{2})\d{2}\d{4}(?=\d{4})|(?<=\d{4}( |-)\d{2})\d{2}\1\d{4}(?=\1\d{4})
Regex Demo
C# Demo
Explanation:
2 alternative regexes
(?<=\d{4}\d{2})\d{2}\d{4}(?=\d{4}) - to handle cardnumbers without any separators (- or <space>)
(?<=\d{4}( |-)\d{2})\d{2}\1\d{4}(?=\1\d{4}) - to handle cardnumbers with separator (- or <space>)
1st Alternative (?<=\d{4}\d{2})\d{2}\d{4}(?=\d{4})
Positive Lookbehind (?<=\d{4}\d{2}) - matches text that has 6 digits immediately behind it
\d{2} matches a digit (equal to [0-9])
{2} Quantifier — Matches exactly 2 times
\d{4} matches a digit (equal to [0-9])
{4} Quantifier — Matches exactly 4 times
Positive Lookahead (?=\d{4}) - matches text that is followed immediately by 4 digits
Assert that the Regex below matches
\d{4} matches a digit (equal to [0-9])
{4} Quantifier — Matches exactly 4 times
2nd Alternative (?<=\d{4}( |-)\d{2})\d{2}\1\d{4}(?=\1\d{4})
Positive Lookbehind (?<=\d{4}( |-)\d{2}) - matches text that has (4 digits followed by a separator followed by 2 digits) immediately behind it
1st Capturing Group ( |-) - get the separator as a capturing group, this is to check the next occurence of the separator using \1
\1 matches the same text as most recently matched by the 1st capturing group (separator, in this case)
Positive Lookahead (?=\1\d{4}) - matches text that is followed by separator and 4 digits
If performance is a concern, here's a pattern that only goes through 94 steps, instead of the other answer's 473, by avoiding lookaround and alternation:
\d{4}[ -]?\d{2}\K\d{2}[ -]?\d{4}
Demo: https://regex101.com/r/0XMluq/4
Edit: In C#'s regex flavor, the following pattern can be used instead, since C# allows variable length lookbehind.
(?<=\d{4}[ -]?\d{2})\d{2}[ -]?\d{4}
Demo
Recently I've to come up with a regular expression for phone number , so as this is already done by many of the devs , i've found a solution related to the same.
And made use of the same in production.
Version#1 is only with numbers:
#"\(?\d{3}\)?-? *\d{3}-? *-?\d{4}"
But then there is an additional requirement: we need to allow special characters like - and ( )
So I modified the regular expression to Version#2 as mentioned below:
#"^([\+]?[0-9]{1}[0-9]{0,2})[\s-]?[\(]?(0?[0-9]\d{0,4}[\)]?[-\s]?)([0-9][\d-\s]{5,7}[\s]?)(x[\d-]{0,4})?$"
Now while validating length of the phone number QA found out that it's accepting more than 16 characters , I've to work again on the regular expression to make it work.
This is where I've found the problem related to Length of the phone number validation using regular expression.
Is there any way to indicate that phone number should accept minimum length as 10 and maximum length as 15 by modifying regular expression ?
Example here could be 123456789 - want to mark this as Invalid phone number as it's having only 9 digits
Example here could be 1234567890123456 - want to mark this as Invalid phone number as it's having only 16 digits
Is there any way to indicate single left parenthesis and single right parenthesis is Invalid inside phone number by modifying regular expression ?
Example here could be 12(34567890 - want to mark this as Invalid phone number , as it's having left parenthesis only
Currently I achieved above things by adding custom attributes to the phone number field. I really want to know , if there's way where we can achieve above things by modifying regular expression itself ?
You may use a regex with a (?=(?:\D*\d){10,15}\D*$) positive lookahead anchored at the start:
^(?=(?:\D*\d){10,15}\D*$)\+?[0-9]{1,3}[\s-]?(?:\(0?[0-9]{1,5}\)|[0-9]{1,5})[-\s]?[0-9][\d\s-]{5,7}\s?(?:x[\d-]{0,4})?$
See the regex demo.
Details:
^ - start of string
(?=(?:\D*\d){10,15}\D*$) - a positive lookahead that makes sure there are 10 to 15 sequences of non-digits followed with 1 digit, and then has 0+ digits up to the end of string
\+? - an optional + symbol
[0-9]{1,3} - 1 to 3 digits
[\s-]? - an optional whitespace or -
(?:\(0?[0-9]{1,5}\)|[0-9]{1,5}) - either of the two alternatives:
\(0?[0-9]{1,5}\) - a (, 1 to 5 digits, )
| - or
[0-9]{1,5} - 1 to 5 digits
[-\s]? - an optional whitespace or -
[0-9] - a digit
[\d\s-]{5,7} - 5 to 7 digits, whitespaces or -
\s? - an optional whitespace
(?:x[\d-]{0,4})? - an optional sequence of:
x - a literal x
[\d-]{0,4} - 0 to 4 digits of -
$ - end of string.
I am looking for Regex expression that will match any of the following:
1.0
2.0
3.1
4.2.1
2.1.1.7
1.3.17.11
12.23.54.18
the nesting/level could be higher than 4 levels...the digits between the dots likely not to exceed 2 digits (last sample).
I tried this #"\d.\d+" but in some cases it did not work.
I am also looking for expression that will match ONLY this:
1.0
12.0
4.0
Here also - no more than 2 digits before the dot.
As usual, think about the structure of what you want to match:
A single digit:
\d
A single number of arbitrary length:
\d+
A single number, constrained to at most 2 digits:
\d{1,2}
A number, followed by a dot, followed by another number:
\d{1,2}\.\d{1,2}
A number, followed by a dot, followed by another number, followed by another dot, followed by yet another number:
\d{1,2}\.\d{1,2}\.\d{1,2}
Notice a pattern? Exactly, you can use grouping and repetition to match that pattern to an arbitrary length:
\d{1,2}(\.\d{1,2})+
Note that . is a meta-character in regular expressions, matching (almost) any character, so to match a literal dot, you need to escape it (as shown above).
To match just two levels of nesting you can constrain the * after the parentheses in a similar manner:
\d{1,2}(\.\d{1,2}){1}
This means it will have to match exactly once. However, in that case you can also simplify to a regex we've seen before:
\d{1,2}\.\d{1,2}
However, putting an exact number of repetitions at the end can be helpful, if you want to create regexes that match n levels of nesting, for arbitrary n.
Try using this
(\d{1,2}[.])+\d{1,2}