By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,665 Members | 1,761 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,665 IT Pros & Developers. It's quick & easy.

Regex Usage, or use Substring?

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a


"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 discussion thread is closed

Replies have been disabled for this discussion.