Write a regex for number notations - c#

I created this regular expression:
^$|^[1-9]+([\.,]\d{0,2})?$
It should accept:
1
11,00
100,88 (error)
100 (error)
11.00
100.88
Shouldn't accept:
0
-5
0,55
0.55
How can I fix it?

You can enclose the whole pattern with an optional group and use a \d instead of the [1-9] and add a (?!0+) negative lookahead restriction to exclude matching values with leading zeros:
^(?!0+)(?:\d+(?:[.,]\d{0,2})?)?$
^^^^^^^^^ ^^
See the regex demo
If you do not want to match 53.-like values, you need to replace {0,2} with {1,2}.
Pattern details:
^ - start of string
(?!0+) - no zeros at the beginning
(?:\d+(?:[.,]\d{0,2})?)? - optional (one or zero) sequence of:
\d+ - 1 or more digits
(?:[.,]\d{0,2})? - optional (one or zero) sequence of:
[.,] - either a . or ,
\d{0,2} - two, one or zero digits
$ - end of string.

So long as the first character is a digit between 1 and 9, subsequent characters can be any digit. However, your expression excludes subsequent 0's; you need to allow for there to be any number of digits so long as the first character is between 1 and 9:
^$|^[1-9]\d*([\.,]\d{0,2})?$
Pattern Explanation:
^ the beginning of the string
[1-9] any digit except "0"
\d* any digit between 0 and unlimited times
([\.,]\d{0,2})?
(optionally) either "." or "," followed by between zero and 2 digits
$ end of string
See this example for further explanation and unit tests.

From my reading of your question you want the string "(error)" to be a valid suffix. Is that right? If so:
^$|^[1-9]+[0-9]*([\.,]\d{0,2})?( \(error\))?$

Related

RegEx for string starts with number and followed by + character

I want a regular expression for such inputs:
1+2
3
1+22+3
But If I write following inputs then it should not allow. Such as;
+1+2
1+
a+1+b+c
12+
The string must start with number and then followed by only + character. But After the + character, it has to be any number.
I tried this [^0-9][^+]? but İt deletes the + sign at the start with the regex I wrote, but there is a problem. While deleting the + character, it also removes the number next to it. This event keeps repeating.
How can I do this?
Please try :
\d+(\+\d)*
Demo: https://regex101.com/r/hfqmYr/2
Where:
\d -> Matches with any digit
+ -> Matches a symbol one or more times
* -> Matching a symbol 0 or many times
As mentioned in the comments, it looks like you can use:
^[0-9]+(?:\+[0-9]+)*$
This is to allow the mentioned sample data and discard those you don't want to allow. See an online demo. The pattern matches:
^ - Start line anchor.
[0-9]+ - 1+ Digits (ASCII).
(?:\+[0-9]+)* - 0+ Times a non-capture group to allow for a literal plus followed by 1+ digits (ASCII).
$ - End line anchor.
As per my knowledge .NET requires you to explicitly mention these ASCII digits to avoid matching numbers from other languages (unless specified otherwise using ECMAScript options).

Split String by Regex Expression

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.

c# regex spain mobile phone

what would be a valid regex for these rules:
34 6xx xxx xxx
34 7yx xxx xxx
(note y cannot be 0 (zero))
would this one work?
34(([6][0-9]{8}$)|([7][1-9]{1}[0-9]{7}$))
Your regular expression should work, assuming that you are not expecting it to handle spaces.
You can further optimize your regex by extracting the common suffix of [0-9]{7} from it:
^34(?:6[0-9]|7[1-9])[0-9]{7}$
If you would like to account for optional spaces, insert \s? into your regex in places where you wish to allow space characters to be inserted:
^34\s?(?:6[0-9]|7[1-9])[0-9]\s?[0-9]{3}\s?[0-9]{3}$
If you need to handle this specific format with spaces, you can use
^34 ?(?:6[0-9]{2}|7[1-9][0-9])(?: ?[0-9]{3}){2}$
See RegexStorm demo
REGEX EXPLANATION:
^ - Start of string
34 ? - 34 followed by an optional space
(?:6[0-9]{2}|7[1-9][0-9]) - A group of 2 alternatives: 6 can be followed by any 2 digits, and 7 can be followed only by non-0 and one more digit
(?: ?[0-9]{3}){2} - 2 groups of 3 digits, optionally separated with a space
$ - End of string.

Replace digits between 2 and 8 in length with specific character using regex

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

C# Regex Match 15 Characters, Single Spaces, Alpha-Numeric

I need to match a string under the following conditions using Regex in C#:
Entire string can only be alphanumeric (including spaces).
Must be a maximum of 15 characters or less (including spaces).
First & last characters can only be a letter.
A single space can appear multiple times in anywhere but the first and last characters of the string. (Multiple spaces together should not be allowed).
Capitalization should be ignored.
Should match the WHOLE word(s).
If any one of these preconditions are broken, a match should not follow.
Here is what i currently have:
^\b([A-z]{1})(([A-z0-9 ])*([A-z]{1}))?\b$
And here are some test strings that should match:
Stack OverFlow
Iamthe greatest
A
superman23s
One Two Three
And some that shouldn't match (note the spaces):
Stack [double_space] Overflow Rocks
23Hello
ThisIsOver15CharactersLong
Hello23
[space_here]hey
etc.
Any suggestions would be much appreciated.
You should use lookaheads
|->matches if all the lookaheads are true
--
^(?=[a-zA-Z]([a-zA-Z\d\s]+[a-zA-Z])?$)(?=.{1,15}$)(?!.*\s{2,}).*$
-------------------------------------- ---------- ----------
| | |->checks if there are no two or more space occuring
| |->checks if the string is between 1 to 15 chars
|->checks if the string starts with alphabet followed by 1 to many requireds chars and that ends with a char that is not space
you can try it here
Try this regex: -
"^([a-zA-Z]([ ](?=[a-zA-Z0-9])|[a-zA-Z0-9]){0,13}[a-zA-Z])$"
Explanation : -
[a-zA-Z] // Match first character letter
( // Capture group
[ ](?=[a-zA-Z0-9]) // Match either a `space followed by non-whitespace` (Avoid double space, but accept single whitespace)
| // or
[a-zA-Z0-9] // just `non-whitespace` characters
){0,13} // from `0 to 13` character in length
[a-zA-Z] // Match last character letter
Update : -
To handle single characters, you can make the pattern after 1st character optional as nicely pointed by #Rawling in comments: -
"^([a-zA-Z](([ ](?=[a-zA-Z0-9])|[a-zA-Z0-9]){0,13}[a-zA-Z])?)$"
^^^ ^^^
use a capture group make it optional
And my version, again using look-aheads:
^(?=.{1,15}$)(?=^[A-Z].*)(?=.*[A-Z]$)(?![ ]{2})[A-Z0-9 ]+$
explained:
^ start of string
(?=.{1,15}$) positive look-ahead: must be between 1 and 15 chars
(?=^[A-Z].*) positive look-ahead: initial char must be alpha
(?=.*[A-Z]$) positive look-ahead: last char must be alpha
(?![ ]{2}) negative look-ahead: string mustn't contain 2 or more consecutive spaces
[A-Z0-9 ]+ if all the look-aheads agree, select only alpha-numeric chars + space
$ end of string
This will also need the IgnoreCase option setting

Categories