I have the date string that was entered incorrectly that I need to correct and would like to know if I could use RegEx.Replace to achieve this.
date string on file is 01.10-2016
I would like to replace the "." and "-" with a "/"
You could use this regular expression: (\d{2})\.(\d{2})-(\d{4})
Breaking it down:
\d - Matches numbers
{2} and {4} - Expects the previous pattern (in our case \d) to appear 2 and 4 times respectively.
( ) - Creates a capture group
\. - . has a special meaning in regular expressions, so we'll escape it.
\. and - are static values, that we expect to be between our capture groups.
So the parts are of our final expression:
(\d{2}) - Match and capture any two digit number from 00 to 99
\. - Match a .
(\d{2}) - Match and capture any two digit number from 00 to 99
- - Match a -
(\d{4}) - Match and capture any four digit number from 0000 to 9999
Try it online
Now, to actually format the date as you want, we need to take the data captured by these 3 capture groups and perform the replacement. You can reference them by their position in the regex, so we have $1, $2, and $3.
var input = "01.10-2016";
var result = Regex.Replace(input, #"(\d{2})\.(\d{2})-(\d{4})", "$1/$2/$3");
This should output your desired values.
Try it online
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
I'm trying to write a regular expression using C#/.Net that matches 1-4 alphanumerics followed by spaces, followed by 10 digits. The catch is the number of spaces plus the number of alphanumerics must equal 4, and the spaces must follow the alphanumerics, not be interspersed.
I'm at a total loss as to how to do this. I can do ^[A-Za-z\d\s]{1,4}[\d]{10}$, but that lets the spaces fall anywhere in the first four characters. Or I could do ^[A-Za-z\d]{1,4}[\s]{0,3}[\d]{10}$ to keep the spaces together, but that would allow more than a total of four characters before the 10 digit number.
Valid:
A12B1234567890
AB1 1234567890
AB 1234567890
Invalid:
AB1 1234567890 (more than 4 characters before the numbers)
A1B1234567890 (less than 4 characters before the numbers)
A1 B1234567890 (space amidst the first 4 characters instead of at the end)
You can force the check with a look-behind (?<=^[\p{L}\d\s]{4}) that will ensure there are four allowed characters before the 10-digits number:
^[\p{L}\d]{1,4}\s{0,3}(?<=^[\p{L}\d\s]{4})\d{10}$
^^^^^^^^^^^^^^^^^^^^
See demo
If you do not plan to support all Unicode letters, just replace \p{L} with [a-z] and use RegexOptions.IgnoreCase.
Here's the regex you need:
^(?=[A-Za-z0-9 ]{4}\d{10}$)[A-Za-z0-9]{1,4} *\d{10}$
It uses a lookahead (?= ) to test if it's followed by 4 chars, either alnum or space, and then it goes back to where it was (the beggining of string, not consuming any chars).
Once that condition is met, the rest is a expression quite similar to what you were trying ([A-Za-z0-9]{1,4} *\d{10}).
Online tester
I know this is dumb, but must work exactly as required.
^[A-Za-z\d]([A-Za-z\d]{3}|[A-Za-z\d]{2}\s|[A-Za-z\d]\s{2}|\s{3})[\d]{10}$
Not sure what you are looking for, but perhaps:
^(?=.{14}$)[A-Za-z0-9]{1,4} *\d{10}
demo
Try this:
Doesn't allow char/space/char combination and starts with a char:
/\b(?!\w\s{1,2}\w+)\w(\w|\s){3}\d{10}/gm
https://regex101.com/r/fF2tR8/2
I'm trying to get the numbers out of a string that user inputs for calculating tip. The query could be "Calculate 18% tip on $25.56" or simply "18% $25.56"
I've built up this regex
.*(\d+)%.*\$(\d+)(\.\d+)?$
However, the first capturing group doesn't capture 18, just 8. What am I doing wrong ?
EDIT: Based on the answer below, I made my regex more error resilient. For matching a string like this
"Calculate18sdaasa%onasda$dsada25dasda.56aaddsadas dsad asdadas 18 sadasd 23 asdasd .56 asdasd"
I now have the regex as
.*?(\d+).*?%.*?\$.*?(\d+).*?(\.\d+).*
This captures the first three numbers 18, 25 and .56 which is what I need.
Note that .* in the beginning of your RegEx is greedy, and that (\d+) will also be satisfied with only matching a single-digit number. Thus, the starting .* kind of "steals" all but the last digit of your first number while (\d+) still matches the last digit of the first number.
Do it like this instead:
.*?(\d+)%.*\$(\d+)(\.\d+)?$
.*? is non-greedy, that means, it will match as few characters as possible to satisfy the pattern.
Try this following regex pattern:
\d+.?\d+
I have asp:TextBox to keep a value of money, i.e. '1000', '1000,0' and '1000,00' (comma is the delimiter because of Russian standard).
What ValidationExpression have I to use into appropriate asp:RegularExpressionValidator?
I tried \d+\,\d{0,2} but it doesn't allows a number without decimal digits, e.g. just '1000'.
\d+(,\d{1,2})?
will allow the comma only when you have decimal digits, and allow no comma at all. The question mark means the same as {0,1}, so after the \d+ you have either zero instances (i.e. nothing) or one instance of
,\d{1,2}
As Helen points out correctly, it will suffice to use a non-capturing group, as in
\d+(?:,\d{1,2})?
The additional ?: means that the parentheses are only meant to group the ,\d{1,2} part for use by the question mark, but that there is no need to remember what was matched within these parenthesis. Since this means less work for the regex enginge, you get a performance boost.
We use this very liberal regular expression for money validation:
new Regex(#"^\-?\(?\$?\s*\-?\s*\(?(((\d{1,3}((\,\d{3})*|\d*))?(\.\d{1,4})?)|((\d{1,3}((\,\d{3})*|\d*))(\.\d{0,4})?))\)?$");
It allows all these:
$0, 0, (0.0000), .1, .01, .0001, $.1, $.01, $.0001, ($.1), ($.01), $(.0001), 0.1, 0.01, 0.0001, 1., 1111., 1,111., 1, 1.00, 1,000.00, $1, $1.00, $1,000.00, $ 1.0000, $ 1.0000, $ 1,000.0000, -1, -1.00, -1,000.00, -$1, -$1.00, -$1,000.00, -$ 1, -$ 1.00, -$ 1,000.00, $-1, $-1.00, $-1,000.00, $(1), $(1.00), $(1,000.00), $ (1), $ (1.00), $ (1,000.00), ($1), ($1.00), ($1,000.00)
http://regexlib.com/Search.aspx?k=money
i used this one in javascript: may be of use for you in c#
var entered = '10.00';
var regex = /^\d+(?:\.\d{2})?$/; // starts with N digits optional ".\d\d"
console.log(entered.match(regex));