c# regex spain mobile phone - c#

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.

Related

How can I match four number fields or a text string using regex

So a sensor I'm interfacing to either outputs 4 multi-digit integers (separated by spaces) or an error string.
Ideally my regex would return a match for either of the above scenarios and reject any
other outputs - e.g. if only 3 numbers are output. I can then check if there are 4 groups (number output) or 1 group (error string output) in the following c#.
The regex I have matches all the time and returns spaces when there are less than 4 numbers so I still need to check everything.
I've tried putting in ?: but the format breaks. Any regex whizzes up to the challenge? Thanks in advance.
([0-9]+\s)([0-9]+\s)([0-9]+\s)([0-9]+)|([a-zA-Z\s_!]+)
So a numeric example would be 11 222 33 4444 or Sensor is in an error state! An incorrect output would be 222 11 3333 as it only has 3 fields
Also - I need to capture the four numbers (but not the spaces) or the error string.
You can capture either the 4 groups with only digits and match the whitespace chars outside of the group.
Or else match 1+ times any of the listed characters in the character class. Note that \s can also match a newline, and as the \s is in the character class the match can also consist of only spaces for example.
To match the whole string, you can add anchors.
^(?:([0-9]+)\s([0-9]+)\s([0-9]+)\s([0-9]+)|[a-zA-Z\s_!]+)$
» Regex demo
Another option to match the error string, is to start matching word characters without digits, optionally repeated by a whitespace char and again word characters without digits.
^(?:([0-9]+)\s([0-9]+)\s([0-9]+)\s([0-9]+)|[^\W\d]+(?:\s+[^\W\d]+)*!?)$
» Regex demo
If there can be a digit in the error message, but you don't want to match only digits or whitespace chars, you can exclude that using a negative lookahead.
^(?:([0-9]+)\s([0-9]+)\s([0-9]+)\s([0-9]+)|(?![\d\s]*$).+)$
» Regex demo

How to use RegEx.Replace to replace the . and - to a / on the following date string 01.04-2016

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

Regular expression - Phone number ( including +, (,), and length validation - C#

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.

Write a regex for number notations

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\))?$

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