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)
Related
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
This is my string.
19282511~2017-08-28 13:24:28~Entering (A/B)~1013~283264/89282511~2017-08-28 13:24:28~Entering (A/B)~1013~283266/79282511~2017-08-28 13:24:28~Entering (A/B)~1013~283261
I would like this string be split like below:
19282511~2017-08-28 13:24:28~Entering (A/B)~1013~283264
89282511~2017-08-28 13:24:28~Entering (A/B)~1013~283266
79282511~2017-08-28 13:24:28~Entering (A/B)~1013~283261
I cannot split my string blindly by slash (/) since there is a value A/B will also get split.
Any idea of doing this by regex expression?
Your help will definitely be appreciated.
You may split with / that is in between digits:
(?<=\d)/(?=\d)
See the regex demo
Details
(?<=\d) - a positive lookbehind that requires a digit to appear immediately to the left of the current location
/ - a / char
(?=\d) - a positive lookahead that requires a digit to appear immediately to the right of the current location.
Since the \d pattern is inside non-consuming patterns, only / will be removed upon splitting and the digits will remain in the resulting items.
Another idea is to match and capture these strings using
/?([^~]*(?:~[^~]*){3}~\d+)
See this regex demo.
Details
/? - 1 or 0 / chars
([^~]*(?:~[^~]*){3}~\d+) - Group 1 (what you need to grab):
[^~]* - zero or more chars other than ~
(?:~[^~]*){3} - 3 or more sequences of ~ and then 0+ chars other than ~
~\d+ - a ~ and then 1 or more digits.
The C# code will look like
var results = Regex.Matches(s, #"/?([^~](?:~[^~]){3}~\d+)")
.Cast()
.Select(m => m.Groups1.Value)
.ToList();
NOTE: By default, \d matches all Unicode digits. If you do not want this behavior, use the RegexOptions.ECMAScript option, or replace \d with [0-9] to only match ASCII digits.
I am trying to validate an input with a regular expression. Up until now all my tests fail and as my experience with regex is limited I thought someone might be able to help me out.
Pattern: digit (possibly "," digit) (possibly ;)
A String may not begin with a ; and not end with a ;.
Digits are allowed to stand alone or with
My regEx (not working): ((\d)(,\d)?)(;?) the problem is it does not seem to check until the end of the string. Also the optional parts are giving me headaches.
Update: ^[0-9]+(,[0-9])?(;[0-9]+(,[0-9])?)+$this seems to work better but it does not match the single digit.
OK:
2,3;4,4;3,2
2,3
2
2,3;3;4,3
NOK:
2,3,,,,
2,3asfafafa
;2,3
2,3;;3,4
2,3;3,4;
Your ^[0-9]+(,[0-9])?(;[0-9]+(,[0-9])?)+$ regex matches 1 or more digits, then an optional sequence of , and 1 digit, followed with one or more similar sequences.
You need to match zero or more comma-separated numbers:
^\d+(?:,\d+)?(?:;\d+(?:,\d+)?)*$
^
See the regex demo
Now, tweaking part:
If only single-digit numbers should be matched, use ^\d(?:,\d)?(?:;\d(?:,\d)?)*$
If the comma-separated number pairs can have the second element empty, add ? after each ,\d (if single digit numbers are to be matched) or * (if the numbers can have more than one digit): ^\d(?:,\d?)?(?:;\d(?:,\d?)?)*$ or ^\d+(?:,\d*)?(?:;\d+(?:,\d*)?)*$.
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. -> ,).
We have a security issue where a specific field in a database has some sensitive information in it. I need a way to detect numbers that are between 2 and 8 in length, replace the digits with a "filler" of the same length.
For instance:
Jim8888Dandy
Mike9999999999Thompson * Note: this is 10 in length and we don't want to replace the digits
123Area Code
Tim Johnson5555555
In these instances anytime we find a number that is between 2 and 8 (inclusive) then I want to replace/fill/substitute that value with the number 0 and keep the length of the original digits
End Result
Jim0000Dandy
Mike9999999999Thompson
000Area Code
Tim Johnson0000000
Is there an easy way to accomplish this using RegEx?
You need to provide a static evaluator method that would do the replacing. It replaces digits in the match with zeroes:
public static string Evaluate(Match m)
{
return Regex.Replace(m.Value, "[0-9]", "0");
}
And then use it with this code:
string input = "9999999099999Thompson534543";
MatchEvaluator evaluator = new MatchEvaluator(Program.Evaluate);
string replaced = Regex.Replace(input, "(?:^|[^0-9])[0-9]{2,8}(?:$|[^0-9])", evaluator);
The regex is:
(?:^|[^0-9]) - should be at the start or preceeded by non-digit
[0-9]{2,8} - the to capture between 2 and 8 digits
(?:$|[^0-9]) - should be at the end or followed by non-digit
Just for the clever regex department. This is not an efficient regex.
(?<=(?>(?'front'\d){0,7}))\d(?=(?'back'(?'-front'\d)){0,7}(?!\d))((?'-front')|(?'-back'))
Replace to 0.
/(?<=(?>(?'front'\d){0,7})) # Measure how many digits we're behind.
\d # This digit is matched
(?=
(?'back' # Measure how many digits we're in front of.
(?'-front'\d)){0,7}
# For every digit here, subtract one group from 'front',
# As to assert we'll never go over the < 8 digit requirement.
(?!\d) # no more digits
)
(
(?'-front') # At least one capturing group left for 'front' or 'back'
|(?'-back') # for > 2 digits requirement.
)/x