need some finer tuning on the regular expression

T

Tony Johansson

Hi!

The regular expression below match these strings.
string text = "(314).555-4000";
//string text = "314-555-4000";
//string text = "314 555-4000";

One small problem is that if I write the text as follows "(314.555-4000";
it still will match the pattern
So how can I write the regular expression so that if one of the parentesis
is missing it should not match the pattern

string pattern = @"\(?\d{3}\)?[-\s.]\d{3}[-.]\d{4}";
MatchCollection matches = Regex.Matches(text, pattern,
RegexOptions.IgnoreCase |
RegexOptions.ExplicitCapture);
Console.WriteLine("hits is {0}", matches.Count);

//Tony
 
J

Jeff Johnson

The regular expression below match these strings.
string text = "(314).555-4000";
//string text = "314-555-4000";
//string text = "314 555-4000";

One small problem is that if I write the text as follows "(314.555-4000";
it still will match the pattern
So how can I write the regular expression so that if one of the parentesis
is missing it should not match the pattern

string pattern = @"\(?\d{3}\)?[-\s.]\d{3}[-.]\d{4}";
MatchCollection matches = Regex.Matches(text, pattern,
RegexOptions.IgnoreCase |
RegexOptions.ExplicitCapture);
Console.WriteLine("hits is {0}", matches.Count);

Use an "or" condition (I'm only demonstrating this part of the regex, so
incorporate it into the whole thing):

(\(\d{3}\)|\d{3})

Now you need either open paren/3 digits/close paren or 3 digits only.
 
H

Hillbilly

Hey, why aren't you giving this guy any of your wrong newsgroup bullsh!t?


Jeff Johnson said:
The regular expression below match these strings.
string text = "(314).555-4000";
//string text = "314-555-4000";
//string text = "314 555-4000";

One small problem is that if I write the text as follows
"(314.555-4000"; it still will match the pattern
So how can I write the regular expression so that if one of the
parentesis is missing it should not match the pattern

string pattern = @"\(?\d{3}\)?[-\s.]\d{3}[-.]\d{4}";
MatchCollection matches = Regex.Matches(text, pattern,
RegexOptions.IgnoreCase |
RegexOptions.ExplicitCapture);
Console.WriteLine("hits is {0}", matches.Count);

Use an "or" condition (I'm only demonstrating this part of the regex, so
incorporate it into the whole thing):

(\(\d{3}\)|\d{3})

Now you need either open paren/3 digits/close paren or 3 digits only.
 
A

Arne Vajhøj

On 23-03-2010 19:57, Hillbilly wrote:
[top posting fixed again again - please don't top post]
Jeff Johnson said:
Tony Johansson said:
The regular expression below match these strings.
string text = "(314).555-4000";
//string text = "314-555-4000";
//string text = "314 555-4000";

One small problem is that if I write the text as follows
"(314.555-4000"; it still will match the pattern
So how can I write the regular expression so that if one of the
parentesis is missing it should not match the pattern

string pattern = @"\(?\d{3}\)?[-\s.]\d{3}[-.]\d{4}";
MatchCollection matches = Regex.Matches(text, pattern,
RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture);
Console.WriteLine("hits is {0}", matches.Count);

Use an "or" condition (I'm only demonstrating this part of the regex,
so incorporate it into the whole thing):

(\(\d{3}\)|\d{3})

Now you need either open paren/3 digits/close paren or 3 digits only.
Hey, why aren't you giving this guy any of your wrong newsgroup bullsh!t?

There are no need to.

The code is C#. The code does not require any special context. The
poster has a proven interest in the C# language. The poster seem
willing to listen.

So even though the question is not one that can be answered
by quoting ECMA-334, then many (probably most) people here are
fine with it.

Believe it or not, but people around here are generally
nice guys/gals.

Arne
 
H

Hillbilly

You too clown boy with your hypocrite bullsh!t. Regular Expressions are not
C#. When they are used with C# they are passed as the argument of a method
just like when a property generated with C# is passed as the value to a
control declared initially declared in HTML that will use that string as the
argument to a method as both of you half-@ss dishonest troll police tried to
pull trying to bullsh!t and bully me about when asking about another type of
expression.

The guy Tony didn't ask about using C# he asked how to write a Regular
Expression. That makes both of you newsgroup troll police dishonest
hypocrites and full of sh!t now trying to weasel out of your hypocrisy. At
least pertains to you as the other troll has wisely chosen to just remain
quiet and hopefully contemplative.

If you want to be a nice guy as you infer quit being a nitpicking @sshole.

Believe it or not.



Arne Vajhøj said:
On 23-03-2010 19:57, Hillbilly wrote:
[top posting fixed again again - please don't top post]
Jeff Johnson said:
The regular expression below match these strings.
string text = "(314).555-4000";
//string text = "314-555-4000";
//string text = "314 555-4000";

One small problem is that if I write the text as follows
"(314.555-4000"; it still will match the pattern
So how can I write the regular expression so that if one of the
parentesis is missing it should not match the pattern

string pattern = @"\(?\d{3}\)?[-\s.]\d{3}[-.]\d{4}";
MatchCollection matches = Regex.Matches(text, pattern,
RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture);
Console.WriteLine("hits is {0}", matches.Count);

Use an "or" condition (I'm only demonstrating this part of the regex,
so incorporate it into the whole thing):

(\(\d{3}\)|\d{3})

Now you need either open paren/3 digits/close paren or 3 digits only.
Hey, why aren't you giving this guy any of your wrong newsgroup
bullsh!t?

There are no need to.

The code is C#. The code does not require any special context. The
poster has a proven interest in the C# language. The poster seem
willing to listen.

So even though the question is not one that can be answered
by quoting ECMA-334, then many (probably most) people here are
fine with it.

Believe it or not, but people around here are generally
nice guys/gals.

Arne
 
H

Hillbilly

You too clown boy with your hypocrite bullsh!t. Regular Expressions are not
C#. When they are used with C# they are passed as the argument of a method
just like when a property generated with C# is passed as the value to a
control declared initially declared in HTML that will use that string as the
argument to a method as both of you half-@ss dishonest troll police tried to
pull trying to bullsh!t and bully me about when asking about another type of
expression.

The guy Tony didn't ask about using C# he asked how to write a Regular
Expression. That makes both of you newsgroup troll police dishonest
hypocrites and full of sh!t now trying to weasel out of your hypocrisy. At
least pertains to you as the other troll has wisely chosen to just remain
quiet and hopefully contemplative.

If you want to be a nice guy as you imply quit being a nitpicking @sshole.

Believe it or not.



Arne Vajhøj said:
On 23-03-2010 19:57, Hillbilly wrote:
[top posting fixed again again - please don't top post]
Jeff Johnson said:
The regular expression below match these strings.
string text = "(314).555-4000";
//string text = "314-555-4000";
//string text = "314 555-4000";

One small problem is that if I write the text as follows
"(314.555-4000"; it still will match the pattern
So how can I write the regular expression so that if one of the
parentesis is missing it should not match the pattern

string pattern = @"\(?\d{3}\)?[-\s.]\d{3}[-.]\d{4}";
MatchCollection matches = Regex.Matches(text, pattern,
RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture);
Console.WriteLine("hits is {0}", matches.Count);

Use an "or" condition (I'm only demonstrating this part of the regex,
so incorporate it into the whole thing):

(\(\d{3}\)|\d{3})

Now you need either open paren/3 digits/close paren or 3 digits only.
Hey, why aren't you giving this guy any of your wrong newsgroup
bullsh!t?

There are no need to.

The code is C#. The code does not require any special context. The
poster has a proven interest in the C# language. The poster seem
willing to listen.

So even though the question is not one that can be answered
by quoting ECMA-334, then many (probably most) people here are
fine with it.

Believe it or not, but people around here are generally
nice guys/gals.

Arne
 
H

Hillbilly

You too clown boy with your hypocrite bullsh!t. Regular Expressions are not
C#. When this type of expression is used with C# it is passed as the
argument of a method
just like when a property generated by C# is passed as the value to a
control that happens to be declared in HTML that will use that string as the
argument to its method as both of you half-@ss dishonest troll police tried
to
pull trying to bullsh!t and bully me when I asked about another type of
expression which I was literally generating using C#.

The guy Tony didn't ask about using C# he asked how to write a Regular
Expression. That makes both of you newsgroup troll police dishonest
hypocrites and full of sh!t now trying to weasel out of your hypocrisy. At
least weasling pertains to you as the other troll has wisely chosen to just
remain quiet and hopefully contemplative.

If you want to be a nice guy as you imply quit being a nitpicking @sshole
or stay out of the discussion if you have nothing useful to contribute.

Believe it or not.



Arne Vajhøj said:
On 23-03-2010 19:57, Hillbilly wrote:
[top posting fixed again again - please don't top post]
Jeff Johnson said:
The regular expression below match these strings.
string text = "(314).555-4000";
//string text = "314-555-4000";
//string text = "314 555-4000";

One small problem is that if I write the text as follows
"(314.555-4000"; it still will match the pattern
So how can I write the regular expression so that if one of the
parentesis is missing it should not match the pattern

string pattern = @"\(?\d{3}\)?[-\s.]\d{3}[-.]\d{4}";
MatchCollection matches = Regex.Matches(text, pattern,
RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture);
Console.WriteLine("hits is {0}", matches.Count);

Use an "or" condition (I'm only demonstrating this part of the regex,
so incorporate it into the whole thing):

(\(\d{3}\)|\d{3})

Now you need either open paren/3 digits/close paren or 3 digits only.
Hey, why aren't you giving this guy any of your wrong newsgroup
bullsh!t?

There are no need to.

The code is C#. The code does not require any special context. The
poster has a proven interest in the C# language. The poster seem
willing to listen.

So even though the question is not one that can be answered
by quoting ECMA-334, then many (probably most) people here are
fine with it.

Believe it or not, but people around here are generally
nice guys/gals.

Arne
 
H

Harlan Messinger

Tony said:
Hi!

The regular expression below match these strings.
string text = "(314).555-4000";
//string text = "314-555-4000";
//string text = "314 555-4000";

One small problem is that if I write the text as follows "(314.555-4000";
it still will match the pattern
So how can I write the regular expression so that if one of the parentesis
is missing it should not match the pattern

string pattern = @"\(?\d{3}\)?[-\s.]\d{3}[-.]\d{4}";
MatchCollection matches = Regex.Matches(text, pattern,
RegexOptions.IgnoreCase |
RegexOptions.ExplicitCapture);
Console.WriteLine("hits is {0}", matches.Count);

I answered this in my response to your previous post on regular expressions.
 
H

Hillbilly

You too clown boy with your hypocrite ca-ca de toro. Regular Expressions are
not C# and you are a liar. When this type of expression is used with C# it
is passed as the argument of a method just like when a property generated by
C# is passed as the value to a control that happens to be declared in HTML
that will use that string as the argument to its method as both of you
half-@ss dishonest troll police tried to pull trying to bully me when I
asked about another type of expression which I was literally generating
using C# which just so happens to be expressed in the page as an HTML
dependency.

The guy Tony didn't ask about using C# he asked how to write a Regular
Expression. That makes both of you newsgroup troll police dishonest
hypocrites and full of ca-ca now trying to weasel out of your hypocrisy. At
least weasling pertains to you Arne as the other troll has wisely chosen to
just remain quiet and hopefully contemplative.

If you want to be a nice guy as you imply quit being a nitpicking @sshole
and stay out of the discussion if you have nothing useful to contribute.

Believe it or not.


Arne Vajhøj said:
On 23-03-2010 19:57, Hillbilly wrote:
[top posting fixed again again - please don't top post]
Jeff Johnson said:
The regular expression below match these strings.
string text = "(314).555-4000";
//string text = "314-555-4000";
//string text = "314 555-4000";

One small problem is that if I write the text as follows
"(314.555-4000"; it still will match the pattern
So how can I write the regular expression so that if one of the
parentesis is missing it should not match the pattern

string pattern = @"\(?\d{3}\)?[-\s.]\d{3}[-.]\d{4}";
MatchCollection matches = Regex.Matches(text, pattern,
RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture);
Console.WriteLine("hits is {0}", matches.Count);

Use an "or" condition (I'm only demonstrating this part of the regex,
so incorporate it into the whole thing):

(\(\d{3}\)|\d{3})

Now you need either open paren/3 digits/close paren or 3 digits only.
Hey, why aren't you giving this guy any of your wrong newsgroup
bullsh!t?

There are no need to.

The code is C#. The code does not require any special context. The
poster has a proven interest in the C# language. The poster seem
willing to listen.

So even though the question is not one that can be answered
by quoting ECMA-334, then many (probably most) people here are
fine with it.

Believe it or not, but people around here are generally
nice guys/gals.

Arne
 
H

Hillbilly

You are dishonest.

Arne Vajhøj said:
On 23-03-2010 19:57, Hillbilly wrote:
[top posting fixed again again - please don't top post]
Jeff Johnson said:
The regular expression below match these strings.
string text = "(314).555-4000";
//string text = "314-555-4000";
//string text = "314 555-4000";

One small problem is that if I write the text as follows
"(314.555-4000"; it still will match the pattern
So how can I write the regular expression so that if one of the
parentesis is missing it should not match the pattern

string pattern = @"\(?\d{3}\)?[-\s.]\d{3}[-.]\d{4}";
MatchCollection matches = Regex.Matches(text, pattern,
RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture);
Console.WriteLine("hits is {0}", matches.Count);

Use an "or" condition (I'm only demonstrating this part of the regex,
so incorporate it into the whole thing):

(\(\d{3}\)|\d{3})

Now you need either open paren/3 digits/close paren or 3 digits only.
Hey, why aren't you giving this guy any of your wrong newsgroup
bullsh!t?

There are no need to.

The code is C#. The code does not require any special context. The
poster has a proven interest in the C# language. The poster seem
willing to listen.

So even though the question is not one that can be answered
by quoting ECMA-334, then many (probably most) people here are
fine with it.

Believe it or not, but people around here are generally
nice guys/gals.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top