Regular expression for IR cell phone number - c#

I am going to validate the IR cell phone number using the regular expression but it does not match it! For example '09126104851'.
What is the problem?
"^09[123456789]{2}(^0[123456789]{1}[0-9]{6}|[123456789]{1}[0-9]{7})$"

Your expression is equivalent to:
^09[1-9]{2}(^0[1-9]{1}[0-9]{6}|[1-9]{1}[0-9]{7})$
Which also can be written shorter as:
^09[1-9]{2}(^0[1-9]{1}\d{6}|[1-9]{1}\d{7})$
Now you've made an error in the middle is ^0. Probably you wanted [^0] ( everything not zero):
^09[1-9]{2}([^0][1-9]{1}\d{6}|[1-9]{1}\d{7})$
But the biggest problem is this part:
09[1-9]{2} matches in total 4 digits
[^0][1-9]{1}\d{6}matches in total 8 digits
[1-9]{1}\d{7} matches also 8 digits in total
So you are trying to match 8+4=12 digits while phone number has 11.
I bet you wanted expression to be:
^09[1-9]{2}([1-9]{1}\d{6}|[1-9]{1}\d{6})$
And now you will notice that alternating matches before and after | are
the same ! So this makes expression even smaller:
^09[1-9]{2}([1-9]{1}\d{6})$
If we can dismiss grouping of last 7 digits - we will notice similar parts:
[1-9]{2} and [1-9]{1}. This lets to reduce expression further:
^09[1-9]{3}\d{6}$
Demo

Because You have ^ symbol in the middle of regular expression.
^ means "in the start of the string".
You need regex from #msd : ^09\d{9}$

Use this regular expression instead: "^09\d{9}$"
For example:
System.Text.RegularExpressions.Regex.IsMatch("09126104851", #"^09\d{9}$")
Check it on fiddle: https://dotnetfiddle.net/8quyvL

Related

Regular Expressions C#

I know there are many questions about making regular expressions, but they all seem to be about a single problem than the general usage. I, too, have a problem like to solve. I have tried to learn by reading about regular expressions, but it gets tricky quick. Here's my question:
C#
I need to validate two textboxes that exist on the same form. The math operations I've coded can handle any floating point number. For this particular application I know of three formats the numbers will be in or there is a mistake on the users behalf. I'd like to prevent those mistakes in example if an extra number is accidentally typed or if enter is hit too early, etc.
Here are the formats: "#.####" "##.####" "###.##" where the "#" represents a mandatory digit. The formats starting with a one or two digit whole number must have 4 trailing digits or more. I've capped it at 8, or so I tried to lol.The format starting with a three digit whole number should never be allowed to have more than two digits trailing the decimal.
Here's what I have tried thus far.
Regex acceptedInputRegex = new Regex(#"^\b[0-9]{3}.[0-9]{2}|[0-9]{1,2}.[0-9]{4,8}$");
Regex acceptedInputRegex = new Regex(#"^\b\d{3}.\d{2} | \d{1,2}.\d{4,8}$");
I have tried it in thinking a match was what I wanted to achieve and as if a match to my negated expression means there is a problem. I was unsuccessful in both attempts. This is the code:
if (acceptedInputRegex.IsMatch(txtMyTextBox1.Text) || acceptedInputRegex.IsMatch(txtMyTextBox2.Text))
{
} else
{
MessageBox.Show("Numbers are not in the right format", "Invalid Input!");
return;
}
Are regular expressions what I should be using to solve this problem?
If not, please tell me what you recommend. If so, please help me correct my regex.
Thanks.
You are close, you need to escape the dots and group the alternatives so that the ^ and $ anchors could be applied to both of them:
#"^(?:\d{3}\.\d{2}|\d{1,2}\.\d{4,8})$"
See the regex demo.
Details:
^ - start of string
(?: - start of a non-capturing group matching either of the two alternatives:
\d{3}\.\d{2} - 3 digits, . and 2 digits
| - or
\d{1,2}\.\d{4,8} - 1 or 2 digits, ., 4 to 8 digits
) - end of the non-capturing group
$ - end of string.
To make \d match only ASCII digits, use RegexOptions.ECMAScript option:
var isValid = Regex.IsMatch(s, #"^(?:\d{3}\.\d{2}|\d{1,2}\.\d{4,8})$", RegexOptions.ECMAScript);

Using Regular Expression to find exact length match multiple times

I need a regular expression to find groups of exactly 8 numbers in a row. The closest I have gotten is:
[0-9]{8}
but it's not exactly what I need. If I had a number that was 9 long it will match the first 8 but I want it to ignore it if it's longer or shorter than 8.
Here are some examples
1234567890 <- no match, it's longer than 8
12345678 <- match: "12345678"
1234567809876543 <- match 1: "12345678", match 2: "09876543" (two groups of 8)
,,111-11-1234,12345678, <- match: "12345678"
To summarize, for every group of exactly 8 numbers make a match.
I'm working with some results of OCR (Optical Character Recognition) and I have to work with the shortcomings of the results so my input can be varied as in the examples above.
Here is some use case data: http://pastebin.com/uijF9K9n
You can use the following regex in .NET:
(?<=^|\D|(?:\d{8})+)\d{8}(?=$|\D|(?:\d{8})+)
See regex demo
It is based on variable-width lookbehind and a lookahead.
Regex breakdown:
(?<=^|\D|(?:\d{8})+) - only if at the string start (^), or preceded with not a digit (\D) or 1 or more sequences of 8 digits ((?:\d{8})+)...
\d{8} - match 8 digits that are followed by...
(?=$|\D|(?:\d{8})+) - either end of string ($) or not a digit (\D) or 1 or more sequences of 8 digits ((?:\d{8})+).
IMPORTANT:
If I got a downvote for the "extra" complexity compared with another answer, note our solutions are different: my regex matches 8-digit number in ID12345678, and the other one does not due to the word boundary.
You can also try this regex
(?:\b|\G)\d{8}(?=(?:\d{8})*\b)
(?:\b|\G) \b match a word boundary | or \G continue where last match attempt ended
\d{8} matches 8 digits [0-9] followed by a lookahead (?=... to check
(?:\d{8})*\b if followed by any amount of {8 digits} until another word boundary
It will match {8 digits} or out of a sequence of such if between two word boundaries.
See demo at regexstorm
\b[0-9]{8}\b this will give you what you want
For more details check this out
http://www.rexegg.com/regex-boundaries.html

regular expression for 4 number, a comma and 4 numbers

Using a regular expression, I would like to match the following string:
4232,2232
I have tried
^[0-9]+(,[0-9]+)$
However, it doesn't work as expected. I want to cater for 4 numbers, a comma and 4 numbers.
You can use the following:
\d{4},\d{4} //or ^\d{4},\d{4}$ with anchors for start and end of string
Explanation:
\d{4} match digits exactly 4 times (\d is shorthand notation for [0-9])
,\d{4} followed by comma and exactly 4 digits again
If i understand you correctly the RegEx you are looking for is basically:
(\d{4},\d{4})
This matches your provided expression as one group.
As an alternative you could write:
([0-9]{4},[0-9]{4})
which has the same result.
In C#, you can use Regex.IsMatch with the \b\d{4},\d{4}\b regex:
var found_value1 = Regex.IsMatch("4232,2232", #"\b\d{4},\d{4}\b");
var found_value2 = Regex.IsMatch("12345,2232", #"\b\d{4},\d{4}\b");
\b makes sure we match whole number.
Output:
true
false

Regular expression for reference number

I need to write the regular expression to fit a specific code pattern: the string to match has 18 integer characters and I need to check if in the first position there is 0 and in the second one 8 or 9. I wrote this expression, but it doesn't work:
Regex regex = new Regex(#"^(.{0}[0]{1}[8,9])(^\d{18}$)");
string compare = "082008014385161873";
if (regex.IsMatch(compare))
{
//true
}
Anyone can help me?
Try this regular expression. It does the following:
Verifies first digit as 0
Verifies second digit as a 8 or 9
Verifies that 16 more digits follow the first 2 digits
.
^0[89]\d{16}$
Use following regular expression:
^0[89]\d{16}$
Alternative using positive lookahead assertion:
^(?=0[89])\d{18}$

Only match regular expression in c# string

I am not too good with regular expressions so this might be an obvious question.
I want my expression to match if a certain number of characters are found and fail if any extra characters are present. For example if I have a string that should have 4 digits the following should be true.
1234 - match
ab1234cd - does not match
012345 - does not match
What I have so far is \d{4} but my understanding is that this would just match any string that has 4 digits together in it anywhere. I want to match only if a string contains 4 digits and nothing else.
Any help would be appreciated. Thanks.
Use ^ and $ to mark the start/end of the string.
Depending on how you are implementing it (single line mode or multiline mode) you can use something similar to:
^\d{4}$
To only match the (beginning of the string) four digits (end of string).
\b[0-9]{4}\b or ^\d{4}$ should both work. Maybe I could expand a little bit on what GrayWizardx said (just in case you do not use Regular Expressions in C# that much), the regular expressions provided above look for lines that have only 4 digits. By default (if memory serves me well), the regular expression engine looks at the first line only, so if you have a string made from more than 1 line and you would like to check the entire string (for instance, the string has been loaded from a file), you would add the option RegexOptions.MultiLine. in this way, the engine will take a look at the other lines as well.
Hope this has been helpful :)
I believe \b[0-9]{4}\b should do the trick.

Categories

Resources