473,385 Members | 1,838 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Regex Usage, or use Substring?

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?
Expand|Select|Wrap|Line Numbers
  1. string s = TextBox1.Text.ToUpper();
  2. string ch = s.Substring(0, 1); // read first character [B,C,X]
  3. if ((ch == "B") || (ch == "C") || (ch == "X")) {
  4. ch = s.Substring(1, 1); // read second character [P,0,5,7]
  5. if ((ch == "P") || (ch == "0") || (ch == "5") || (ch == "7")) {
  6. if (12 < len) {
  7. ch = " "; // check for spaces in 2 places
  8. if ((s.Substring(7, 1) == ch) && (s.Substring(12, 1) == ch)) {
  9. if (len == 15) {
  10. ok = IsNumeric(new string[] { s.Substring(2, 5), s.Substring(8,
  11. 4), s.Substring(13, 2) });
  12. } else {
  13. Console.WriteLine("Non-numeric data found in numeric section.");
  14. }
  15. }
  16. } else {
  17. Console.WriteLine("There should be blanks at positions 8 and 13.");
  18. ok = false;
  19. }
  20. }
  21. } else {
  22. Console.WriteLine("Second letter should be [P, 0, 5, or 7].");
  23. ok = false;
  24. }
  25. } else {
  26. Console.WriteLine("First letter should be [B, C, or X].");
  27. ok = false;
  28. }
  29.  
Thanks in advance for your help and your time.
Jun 27 '08 #1
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
Jun 27 '08 #2
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?
Expand|Select|Wrap|Line Numbers
  1. string s = TextBox1.Text.ToUpper();
  2. string ch = s.Substring(0, 1); // read first character [B,C,X]
  3. if ((ch == "B") || (ch == "C") || (ch == "X")) {
  4.   ch = s.Substring(1, 1); // read second character [P,0,5,7]
  5.   if ((ch == "P") || (ch == "0") || (ch == "5") || (ch == "7")) {
  6.     if (12 < len) {
  7.       ch = " "; // check for spaces in 2 places
  8.       if ((s.Substring(7, 1) == ch) && (s.Substring(12, 1) == ch)) {
  9.         if (len == 15) {
  10.           ok = IsNumeric(new string[] { s.Substring(2, 5), s.Substring(8,
  11. 4), s.Substring(13, 2) });
  12.           } else {
  13.             Console.WriteLine("Non-numeric data found in numeric section.");
  14.           }
  15.         }
  16.       } else {
  17.         Console.WriteLine("There should be blanks at positions 8 and 13.");
  18.         ok = false;
  19.       }
  20.     }
  21.   } else {
  22.     Console.WriteLine("Second letter should be [P, 0, 5, or 7].");
  23.     ok = false;
  24.   }
  25. } else {
  26.   Console.WriteLine("First letter should be [B, C, or X].");
  27.   ok = false;
  28. }
  29.  
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
Jun 27 '08 #3


"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
Jun 27 '08 #4

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

Similar topics

2
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"...
7
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...
5
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...
4
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,...
17
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',''...
4
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
16
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)...
4
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....
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...
0
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,...
0
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$) { } ...
0
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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...
0
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,...
0
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...
0
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,...
0
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...

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.