473,547 Members | 2,290 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

REGEX. What am I doing wrong?

Hello,

I need to create a REGEX which accepts only phone numbers.

The phone numbers start allways with 261, 21, 96 or 91 and have 7
numbers after it.

Something like. 261 1223346, 21 2334456, etc.

I tried the following but it is not working:
^(?:261|21|96|9 1)\s\d{7}$

How can I do this?

Thanks,

Miguel

Feb 26 '07 #1
11 1139
On Feb 26, 2:11 pm, "shapper" <mdmo...@gmail. comwrote:
Hello,

I need to create a REGEX which accepts only phone numbers.

The phone numbers start allways with 261, 21, 96 or 91 and have 7
numbers after it.

Something like. 261 1223346, 21 2334456, etc.

I tried the following but it is not working:
^(?:261|21|96|9 1)\s\d{7}$
if (?:) means a noncapturing group then you need a parenthesis

^?:(261|21|96|9 1)\s\d{7}$

Feb 26 '07 #2
Hi there again,

I believe it should work. I'm note sure how you utilized this regular
expression but if you're trying validate user input via regular expression
validation control
ValidationExpre ssion="^(261|21 |96|91)\s*\d{7} $"
should be enough. Otherwise, if you try to validate and get both area code
this might be an idea how to do that:

string pattern = @"^(261|21|96|9 1)\s(\d{7})$";

System.Text.Reg ularExpressions .Match match =
System.Text.Reg ularExpressions .Regex.Match(
txtPhone.Text, pattern);

if (match.Success)
{
string areaCode = match.Groups[1].Value;
string number = match.Groups[2].Value;
}
else
{
// not valid
}
--

Hope this helps

Milosz
"shapper" wrote:
Hello,

I need to create a REGEX which accepts only phone numbers.

The phone numbers start allways with 261, 21, 96 or 91 and have 7
numbers after it.

Something like. 261 1223346, 21 2334456, etc.

I tried the following but it is not working:
^(?:261|21|96|9 1)\s\d{7}$

How can I do this?

Thanks,

Miguel

Feb 26 '07 #3
^((261)|(21)|(9 6)|(91))\s\d{7} $

works for me
Peter

"shapper" <md*****@gmail. comwrote in message
news:11******** **************@ m58g2000cwm.goo glegroups.com.. .
Hello,

I need to create a REGEX which accepts only phone numbers.

The phone numbers start allways with 261, 21, 96 or 91 and have 7
numbers after it.

Something like. 261 1223346, 21 2334456, etc.

I tried the following but it is not working:
^(?:261|21|96|9 1)\s\d{7}$

How can I do this?

Thanks,

Miguel

Feb 26 '07 #4
On Feb 26, 2:44 pm, "Peter Bradley" <pbrad...@uwic. ac.ukwrote:
^((261)|(21)|(9 6)|(91))\s\d{7} $

works for me

Peter

"shapper" <mdmo...@gmail. comwrote in message

news:11******** **************@ m58g2000cwm.goo glegroups.com.. .
Hello,
I need to create a REGEX which accepts only phone numbers.
The phone numbers start allways with 261, 21, 96 or 91 and have 7
numbers after it.
Something like. 261 1223346, 21 2334456, etc.
I tried the following but it is not working:
^(?:261|21|96|9 1)\s\d{7}$
How can I do this?
Thanks,
Miguel- Hide quoted text -

- Show quoted text -
http://groups.google.com/group/micro...fc7a34ed155ad8

Feb 26 '07 #5
On Feb 26, 1:44 pm, "Peter Bradley" <pbrad...@uwic. ac.ukwrote:
^((261)|(21)|(9 6)|(91))\s\d{7} $

works for me

Peter

"shapper" <mdmo...@gmail. comwrote in message

news:11******** **************@ m58g2000cwm.goo glegroups.com.. .
Hello,
I need to create a REGEX which accepts only phone numbers.
The phone numbers start allways with 261, 21, 96 or 91 and have 7
numbers after it.
Something like. 261 1223346, 21 2334456, etc.
I tried the following but it is not working:
^(?:261|21|96|9 1)\s\d{7}$
How can I do this?
Thanks,
Miguel
Hi,

It really does not work. Please try:

' Create new regex
Dim regex As System.Text.Reg ularExpressions .Regex
regex = New RegularExpressi ons.Regex("^((2 61)|(21)|(96)|( 91))\s
\d{7}$")

' Check if expression is a match
Response.Write( regex.IsMatch(" 961234567").ToS tring)

It returns False.

Any idea?

Thanks,
Miguel

Feb 26 '07 #6
On Feb 26, 1:49 pm, "Alexey Smirnov" <alexey.smir... @gmail.comwrote :
On Feb 26, 2:44 pm, "Peter Bradley" <pbrad...@uwic. ac.ukwrote:
^((261)|(21)|(9 6)|(91))\s\d{7} $
works for me
Peter
"shapper" <mdmo...@gmail. comwrote in message
news:11******** **************@ m58g2000cwm.goo glegroups.com.. .
Hello,
I need to create a REGEX which accepts only phone numbers.
The phone numbers start allways with 261, 21, 96 or 91 and have 7
numbers after it.
Something like. 261 1223346, 21 2334456, etc.
I tried the following but it is not working:
^(?:261|21|96|9 1)\s\d{7}$
How can I do this?
Thanks,
Miguel- Hide quoted text -
- Show quoted text -

http://groups.google.com/group/micro...framework.aspn...
Hi Alexey,

I also tried your suggestion and a few others and it does not work.
Any idea?

Thanks,
Miguel

Feb 26 '07 #7
On Feb 26, 3:32 pm, "shapper" <mdmo...@gmail. comwrote:
On Feb 26, 1:49 pm, "Alexey Smirnov" <alexey.smir... @gmail.comwrote :


On Feb 26, 2:44 pm, "Peter Bradley" <pbrad...@uwic. ac.ukwrote:
^((261)|(21)|(9 6)|(91))\s\d{7} $
works for me
Peter
"shapper" <mdmo...@gmail. comwrote in message
>news:11******* *************** @m58g2000cwm.go oglegroups.com. ..
Hello,
I need to create a REGEX which accepts only phone numbers.
The phone numbers start allways with 261, 21, 96 or 91 and have 7
numbers after it.
Something like. 261 1223346, 21 2334456, etc.
I tried the following but it is not working:
^(?:261|21|96|9 1)\s\d{7}$
How can I do this?
Thanks,
Miguel- Hide quoted text -
- Show quoted text -
http://groups.google.com/group/micro...framework.aspn...

Hi Alexey,

I also tried your suggestion and a few others and it does not work.
because of \s what mean a single space

regex = New RegularExpressi ons.Regex("^((2 61)|(21)|(96)|( 91))\d{7}$")

Feb 26 '07 #8
The examples the OP gave were (I copy and paste):

261 1223346, 21 2334456

I assume that means a space is needed between the prefix and the last 7
digits.

So it still works for me on that basis.
Peter
Hi,

It really does not work. Please try:

' Create new regex
Dim regex As System.Text.Reg ularExpressions .Regex
regex = New RegularExpressi ons.Regex("^((2 61)|(21)|(96)|( 91))\s
\d{7}$")

' Check if expression is a match
Response.Write( regex.IsMatch(" 961234567").ToS tring)

It returns False.

Any idea?

Thanks,
Miguel

Feb 26 '07 #9
You should have told us space is optional:

string pattern = @"^(261|21|96|9 1)\s*\d{7}$";

should do the job.
--
Milosz
"shapper" wrote:
On Feb 26, 1:44 pm, "Peter Bradley" <pbrad...@uwic. ac.ukwrote:
^((261)|(21)|(9 6)|(91))\s\d{7} $

works for me

Peter

"shapper" <mdmo...@gmail. comwrote in message

news:11******** **************@ m58g2000cwm.goo glegroups.com.. .
Hello,
I need to create a REGEX which accepts only phone numbers.
The phone numbers start allways with 261, 21, 96 or 91 and have 7
numbers after it.
Something like. 261 1223346, 21 2334456, etc.
I tried the following but it is not working:
^(?:261|21|96|9 1)\s\d{7}$
How can I do this?
Thanks,
Miguel

Hi,

It really does not work. Please try:

' Create new regex
Dim regex As System.Text.Reg ularExpressions .Regex
regex = New RegularExpressi ons.Regex("^((2 61)|(21)|(96)|( 91))\s
\d{7}$")

' Check if expression is a match
Response.Write( regex.IsMatch(" 961234567").ToS tring)

It returns False.

Any idea?

Thanks,
Miguel

Feb 26 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
4406
by: Sriram | last post by:
Hi, I am having trouble matching a regex that combines a negated character class and an anchor ($). Basically, I want to match all strings that don't end in a digit. So I tried: bash-2.05a@bermuda:15$perl -e 'while (<STDIN>) { if (/$/) { print;}}' skdsklds skdsklds
8
5547
by: Bibe | last post by:
I've been trying to get this going for awhile now, and need help. I've done a regex object, and when I use IsMatch, it's behavior is quite weird. I am trying to use Regex to make sure that a variable is only alphanumeric (no strange characters). Here's the code: Regex regExp = new Regex("*");
1
3410
by: kevin | last post by:
I am trying to strip the outermost html tag by capturing this tag with regex and then using the string replace function to replace it with an empty string. while stepping through the code, RegEx returns the entire input string although testing this in The Regulator returns just what I want. What am I doing wrong here?...
2
1201
by: George Durzi | last post by:
Consider the following HTML snippet. I want to extract the section shown below. <!-- some html --> <TABLE WIDTH=100% CELLPADDING=0 CELLSPACING=0 border=0><?xml version=1.0 encoding=UTF-16?> ** There is some HTML here that I want to extract ** </TABLE> <!-- some html -->
6
1766
by: tshad | last post by:
Is there a way to use Regex inside of a tag, such as asp:label? I tried something like this but can't make it work: <asp:label id="Phone" text=Regex.Replace('<%# Container.DataItem("Phone") %>',"(\d{3})(\d{3})(\d{4})","($1) $2-$3") runat="server"/> I have this inside my Repeater and want it to filter the field during bind. I can do it...
4
2626
by: Chris | last post by:
Hi Everyone, I am using a regex to check for a string. When all the file contains is my test string the regex returns a match, but when I embed the test string in the middle of a text file a match is never returned. The string that I give to the regex is one that contains the entire contents of a text file. I'm using the multi-line...
10
1853
by: bullockbefriending bard | last post by:
first, regex part: I am new to regexes and have come up with the following expression: ((1|),(1|)/){5}(1|),(1|) to exactly match strings which look like this: 1,2/3,4/5,6/7,8/9,10/11,12 i.e. 6 comma-delimited pairs of integer numbers separated by the
5
2994
by: Knight | last post by:
Hi, in the following program, compiled using gcc on Linux, and invoked as a.out '*' '111' I get '111' matches '*'. What am I doing wrong? #include <sys/types.h> #include <stdio.h> #include <stdlib.h> #include <regex.h>
8
1336
by: adamisko | last post by:
Hello, what is wrong with this code, I want to divide string to pieces 4 chars length. string tekst = "divide string to 4 chars pieces"; Regex regex = new Regex("(.{1,4})"); string substrings = regex.Split(tekst); but instead of array with: "divi","de s","trin" etc.. i have
0
7507
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7698
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7947
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7461
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5361
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5080
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3472
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1922
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
747
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.