Two part question:
1. Is Regex more efficient than manually comparing values using Substring?
2. I've never created a Regex expression. How would I use regex to do the
equivalent of what I have coded using Substrings below? -
string s = TextBox1.Text.ToUpper();
-
string ch = s.Substring(0, 1); // read first character [B,C,X]
-
if ((ch == "B") || (ch == "C") || (ch == "X")) {
-
ch = s.Substring(1, 1); // read second character [P,0,5,7]
-
if ((ch == "P") || (ch == "0") || (ch == "5") || (ch == "7")) {
-
if (12 < len) {
-
ch = " "; // check for spaces in 2 places
-
if ((s.Substring(7, 1) == ch) && (s.Substring(12, 1) == ch)) {
-
if (len == 15) {
-
ok = IsNumeric(new string[] { s.Substring(2, 5), s.Substring(8,
-
4), s.Substring(13, 2) });
-
} else {
-
Console.WriteLine("Non-numeric data found in numeric section.");
-
}
-
}
-
} else {
-
Console.WriteLine("There should be blanks at positions 8 and 13.");
-
ok = false;
-
}
-
}
-
} else {
-
Console.WriteLine("Second letter should be [P, 0, 5, or 7].");
-
ok = false;
-
}
-
} else {
-
Console.WriteLine("First letter should be [B, C, or X].");
-
ok = false;
-
}
-
Thanks in advance for your help and your time. 3 2564
jp2msft <jp*****@discussions.microsoft.comwrote:
Two part question:
1. Is Regex more efficient than manually comparing values using Substring?
Sometimes.
2. I've never created a Regex expression. How would I use regex to do the
equivalent of what I have coded using Substrings below?
Well, let's start off by getting rid of the unnecessary substrings,
when you're only looking at a single character:
string s = TextBox1.Text.ToUpper();
char ch = s[0]; // read first character [B,C,X]
ok = false; // Only one path can produce ok = true
if (ch=='B' || ch=='C' || ch=='X')
{
ch = s[1]; // read second character [P,0,5,7]
if (ch == 'P' || ch == '0' || ch == '5' || ch == '7'))
{
if (len 12) // Reversed if (12 < len) for readability
{
if (s[7] == ' ' && s[12] == ' ')
{
if (len == 15)
{
ok = IsNumeric(new string[] { s.Substring(2, 5),
s.Substring(8, 4),
s.Substring(13, 2) });
}
else
{
// This message looks incorrect - we've just checked
// the length, not the data
Console.WriteLine
("Non-numeric data found in numeric section");
}
}
else
{
Console.WriteLine
("There should be blanks at positions 8 and 13.");
}
}
}
else
{
Console.WriteLine("Second letter should be [P, 0, 5, or 7].");
}
}
else
{
Console.WriteLine("First letter should be [B, C, or X].");
}
Okay, now assuming that the IsNumeric call is actually to check whether
all of those characters are digits, we could indeed convert all of this
into a regular expression - but it would either match or not. It
wouldn't say which part failed. The regex would be something like:
@"^[BCH][P057]\d\d\d\d\d \d\d\d\d \d\d$"
I haven't tested it, but it looks okay to inspection...
--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon_skeet
C# in Depth: http://csharpindepth.com
jp2msft wrote:
Two part question:
1. Is Regex more efficient than manually comparing values using Substring?
Only if you write the manual comparing code poorly.
2. I've never created a Regex expression. How would I use regex to do the
equivalent of what I have coded using Substrings below? -
string s = TextBox1.Text.ToUpper();
-
string ch = s.Substring(0, 1); // read first character [B,C,X]
-
if ((ch == "B") || (ch == "C") || (ch == "X")) {
-
ch = s.Substring(1, 1); // read second character [P,0,5,7]
-
if ((ch == "P") || (ch == "0") || (ch == "5") || (ch == "7")) {
-
if (12 < len) {
-
ch = " "; // check for spaces in 2 places
-
if ((s.Substring(7, 1) == ch) && (s.Substring(12, 1) == ch)) {
-
if (len == 15) {
-
ok = IsNumeric(new string[] { s.Substring(2, 5), s.Substring(8,
-
4), s.Substring(13, 2) });
-
} else {
-
Console.WriteLine("Non-numeric data found in numeric section.");
-
}
-
}
-
} else {
-
Console.WriteLine("There should be blanks at positions 8 and 13.");
-
ok = false;
-
}
-
}
-
} else {
-
Console.WriteLine("Second letter should be [P, 0, 5, or 7].");
-
ok = false;
-
}
-
} else {
-
Console.WriteLine("First letter should be [B, C, or X].");
-
ok = false;
-
}
-
This is a good example of something that should be done as regex,
because the regex will be much easier to read and maintain.
"[BCX][P057]\d{5} \d{4} \d{2}"
should work.
Arne
"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP*********************@msnews.microsoft.com. ..
jp2msft <jp*****@discussions.microsoft.comwrote:
>Two part question:
1. Is Regex more efficient than manually comparing values using Substring?
Sometimes.
>2. I've never created a Regex expression. How would I use regex to do the equivalent of what I have coded using Substrings below?
Well, let's start off by getting rid of the unnecessary substrings,
when you're only looking at a single character:
string s = TextBox1.Text.ToUpper();
char ch = s[0]; // read first character [B,C,X]
ok = false; // Only one path can produce ok = true
if (ch=='B' || ch=='C' || ch=='X')
{
ch = s[1]; // read second character [P,0,5,7]
if (ch == 'P' || ch == '0' || ch == '5' || ch == '7'))
{
if (len 12) // Reversed if (12 < len) for readability
{
if (s[7] == ' ' && s[12] == ' ')
{
if (len == 15)
{
ok = IsNumeric(new string[] { s.Substring(2, 5),
s.Substring(8, 4),
s.Substring(13, 2) });
}
else
{
// This message looks incorrect - we've just checked
// the length, not the data
Console.WriteLine
("Non-numeric data found in numeric section");
}
}
else
{
Console.WriteLine
("There should be blanks at positions 8 and 13.");
}
}
}
else
{
Console.WriteLine("Second letter should be [P, 0, 5, or 7].");
}
}
else
{
Console.WriteLine("First letter should be [B, C, or X].");
}
Okay, now assuming that the IsNumeric call is actually to check whether
all of those characters are digits, we could indeed convert all of this
into a regular expression - but it would either match or not. It
wouldn't say which part failed. The regex would be something like:
@"^[BCH][P057]\d\d\d\d\d \d\d\d\d \d\d$"
I haven't tested it, but it looks okay to inspection...
Jon, I believe the first character should be B, C, or X, not H... other
than that, looks ok to me :)
Mythran This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: mikea59 |
last post by:
I am getting errors in XMLSpy (Pro) in the following case:
Source Document:
<test> 12345 AB 12345 </test>
Stylesheet:
<xsl:stylesheet version="2.0"...
|
by: alphatan |
last post by:
Is there relative source or document for this purpose?
I've searched the index of "Mastering Regular Expression", but cannot
get the useful information for C.
Thanks in advanced.
--
Learning...
|
by: Tap |
last post by:
I would like to parse the following string with as little code as possible.
stringValue = "Email Message ID:TAPASVI to...
|
by: Cor |
last post by:
Hi Newsgroup,
I have given an answer in this newsgroup about a "Replace".
There came an answer on that I did not understand, so I have done some
tests.
I got the idea that someone said,...
|
by: steve |
last post by:
here's the deal...cvs, tick encapsulted data. trying to use regex's to
validate records. here's an example row:
'AD,'BF','132465','06/09/2004','','BNSF','A','TYPE','1278','','BR','2999',''...
|
by: Brian Henry |
last post by:
I have phone numbers like this in a data table
123-435-1234
1231231234
432.234.2321
they all have different formatting, what I want to do is get them all
formatted like this
(123) 123-1234
|
by: Mark Chambers |
last post by:
Hi there,
I'm seeking opinions on the use of regular expression searching. Is there
general consensus on whether it's now a best practice to rely on this rather
than rolling your own (string)...
|
by: Flomo Togba Kwele |
last post by:
I am having difficulty writing a Regex constructor.
A line has a quote(") at its beginning and its end. I need to strip both characters off. If the
line looks like "1", I need the result to be 1....
|
by: jonnyboy6969 |
last post by:
Hi All
Really hoping someone can help me out here with my deficient regex skills :)
I have a function which takes a string of HTML and replaces a term (word or phrase) with a link. The pupose...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
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,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
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,...
|
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...
| | |