473,322 Members | 1,425 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,322 software developers and data experts.

VB pgmr needs help with Regex for C#

Hi All,
I am a VB 6 programmer who is now trying to learn C#. In doing so, I
am trying to convert some of my VB modules to C#. I routinely user Reg
Expressions in VB and am having some trouble trying to use Regex in
C#. Basically, I have a fixed format text file which I need to
validate prior to using in a program. The validation insures the data
format matches what the program is expecting to find in the file. The
pattern I am trying to match for multiple lines is "^[0-9]{4}.{74}01$"
or IOW, 4 digits at the start of a line, followed by 74 characters,
and ending with a literal "01" at the end of the line. This pattern
works fine in my VB code and it correctly identifies all of the lines
with this pattern. My C# code, on the other hand, finds 0 matches for
the same file.

I'm quite sure I am missing something quite simple but I just can't
see what it is! Can some kind soul please point out where I am going
wrong? I am including the code snippets for my VB app and for the C#
app. BTW, the first several lines of data do NOT contain the matching
data. The data does contain 345 lines of matching data which VB does
find.

VB
Private Sub Command1_Click()
Const FILENAME As String = "D:\Johnw\Data\RPLs\V2 RPLs\alldata.rpl"
Dim regx1 As New RegExp
Dim m As Match, mc As MatchCollection, sm As SubMatches
Dim sText As String
Dim fs As New FileSystemObject
Dim ts As TextStream
Set ts = fs.OpenTextFile(FILENAME)
sText = ts.ReadAll
With regx1
.Global = True
.MultiLine = True
.Pattern = "^[0-9]{4}.{74}01$"
Set mc = .Execute(sText)
Debug.Print mc.Count
End With

End Sub

C# App
private string ValidateFile(string filename)
{
string alltext = new StreamReader
(@"D:\Johnw\Data\RPLs\V2 RPLs\alldata.rpl").ReadToEnd();
Regex re = new Regex("^[0-9]{4}.{74}01$", RegexOptions.Multiline);

MatchCollection mc = re.Matches(alltext);
Console.WriteLine("Found " + mc.Count.ToString() + " matches");
if ( mc.Count == 0 )
{
return "";
}
return filename;
}

--
John Wood a.k.a Mortimer Schnurd
http://www.loosemarbles.com
Nov 15 '05 #1
2 1629
On Mon, 01 Mar 2004 17:46:43 -0500, in msg
<5n********************************@4ax.com>, Mortimer Schnurd
<fu**********@hotsmail.com> wrote:
Hi All,
I am a VB 6 programmer who is now trying to learn C#. In doing so, I
am trying to convert some of my VB modules to C#. I routinely user Reg
Expressions in VB and am having some trouble trying to use Regex in
C#. Basically, I have a fixed format text file which I need to
validate prior to using in a program. The validation insures the data
format matches what the program is expecting to find in the file. The
pattern I am trying to match for multiple lines is "^[0-9]{4}.{74}01$"
or IOW, 4 digits at the start of a line, followed by 74 characters,
and ending with a literal "01" at the end of the line. This pattern
works fine in my VB code and it correctly identifies all of the lines
with this pattern. My C# code, on the other hand, finds 0 matches for
the same file.

I'm quite sure I am missing something quite simple but I just can't
see what it is! Can some kind soul please point out where I am going
wrong? I am including the code snippets for my VB app and for the C#
app. BTW, the first several lines of data do NOT contain the matching
data. The data does contain 345 lines of matching data which VB does
find.

VB
Private Sub Command1_Click()
Const FILENAME As String = "D:\Johnw\Data\RPLs\V2 RPLs\alldata.rpl"
Dim regx1 As New RegExp
Dim m As Match, mc As MatchCollection, sm As SubMatches
Dim sText As String
Dim fs As New FileSystemObject
Dim ts As TextStream
Set ts = fs.OpenTextFile(FILENAME)
sText = ts.ReadAll
With regx1
.Global = True
.MultiLine = True
.Pattern = "^[0-9]{4}.{74}01$"
Set mc = .Execute(sText)
Debug.Print mc.Count
End With

End Sub

C# App
private string ValidateFile(string filename)
{
string alltext = new StreamReader
(@"D:\Johnw\Data\RPLs\V2 RPLs\alldata.rpl").ReadToEnd();
Regex re = new Regex("^[0-9]{4}.{74}01$", RegexOptions.Multiline);

MatchCollection mc = re.Matches(alltext);
Console.WriteLine("Found " + mc.Count.ToString() + " matches");
if ( mc.Count == 0 )
{
return "";
}
return filename;
}

Well, after reducing mi pattern to its most elemental state then
iteratively adding more to the patter, I found the answer to my
problem: it seems that, when using multiline, VB 6 doesn't give a
rat's-a$$ about a carriage-return character/new-line character pair
and it treats it as one character when looking for an end-of-line "$".
Whereas, C# does care and a "\r" needs to be accounted for within the
pattern. Changing my pattern to "^[0-9]{4}.{74}01\r$" now finds all
occurrences of the pattern in my file.
--
John Wood a.k.a Mortimer Schnurd
http://www.loosemarbles.com
Nov 15 '05 #2
Take a look at O'Reilly Book 'Mastering Regular Expression' ISBN
0-596-00289-0. It covers .NET Regex.

Mortimer Schnurd wrote:
On Mon, 01 Mar 2004 17:46:43 -0500, in msg
<5n********************************@4ax.com>, Mortimer Schnurd
<fu**********@hotsmail.com> wrote:

Hi All,
I am a VB 6 programmer who is now trying to learn C#. In doing so, I
am trying to convert some of my VB modules to C#. I routinely user Reg
Expressions in VB and am having some trouble trying to use Regex in
C#. Basically, I have a fixed format text file which I need to
validate prior to using in a program. The validation insures the data
format matches what the program is expecting to find in the file. The
pattern I am trying to match for multiple lines is "^[0-9]{4}.{74}01$"
or IOW, 4 digits at the start of a line, followed by 74 characters,
and ending with a literal "01" at the end of the line. This pattern
works fine in my VB code and it correctly identifies all of the lines
with this pattern. My C# code, on the other hand, finds 0 matches for
the same file.

I'm quite sure I am missing something quite simple but I just can't
see what it is! Can some kind soul please point out where I am going
wrong? I am including the code snippets for my VB app and for the C#
app. BTW, the first several lines of data do NOT contain the matching
data. The data does contain 345 lines of matching data which VB does
find.

VB
Private Sub Command1_Click()
Const FILENAME As String = "D:\Johnw\Data\RPLs\V2 RPLs\alldata.rpl"
Dim regx1 As New RegExp
Dim m As Match, mc As MatchCollection, sm As SubMatches
Dim sText As String
Dim fs As New FileSystemObject
Dim ts As TextStream
Set ts = fs.OpenTextFile(FILENAME)
sText = ts.ReadAll
With regx1
.Global = True
.MultiLine = True
.Pattern = "^[0-9]{4}.{74}01$"
Set mc = .Execute(sText)
Debug.Print mc.Count
End With

End Sub

C# App
private string ValidateFile(string filename)
{
string alltext = new StreamReader
(@"D:\Johnw\Data\RPLs\V2 RPLs\alldata.rpl").ReadToEnd();
Regex re = new Regex("^[0-9]{4}.{74}01$", RegexOptions.Multiline);

MatchCollection mc = re.Matches(alltext);
Console.WriteLine("Found " + mc.Count.ToString() + " matches");
if ( mc.Count == 0 )
{
return "";
}
return filename;
}


Well, after reducing mi pattern to its most elemental state then
iteratively adding more to the patter, I found the answer to my
problem: it seems that, when using multiline, VB 6 doesn't give a
rat's-a$$ about a carriage-return character/new-line character pair
and it treats it as one character when looking for an end-of-line "$".
Whereas, C# does care and a "\r" needs to be accounted for within the
pattern. Changing my pattern to "^[0-9]{4}.{74}01\r$" now finds all
occurrences of the pattern in my file.
--
John Wood a.k.a Mortimer Schnurd
http://www.loosemarbles.com

Nov 15 '05 #3

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

Similar topics

19
by: David Logan | last post by:
We need an additional function in the String class. We need the ability to suppress empty fields, so that we can more effectively parse. Right now, multiple whitespace characters create multiple...
7
by: Extremest | last post by:
I am using this regex. static Regex paranthesis = new Regex("(\\d*/\\d*)", RegexOptions.IgnoreCase); it should find everything between parenthesis that have some numbers onyl then a forward...
2
by: Smokey Grindel | last post by:
I have an app that builds where clauses (its 3rd party) and for some reason it adds an m after decimal numbers... so I get results like this = 45.1234m And Like '123%' And = '45.1234m' ...
0
by: Guoqi Zheng | last post by:
Dear Sir, I need to use regex to replace some string. Below is what I use. output = "sample data <href=""xlink:GG44-33"">, part two <href=""xlink:GG55-123"">" Dim regEx Set regEx = New...
0
by: Itanium | last post by:
Hi all. I need to recognize some special keywords in my app. I usually accomplish this task with a regex construction like this… \bkeyword\b …that means “match the keyword if it is preceded...
6
by: mohaaron | last post by:
Hello all, I'm not very good with writing regular expressions and need some help with this one. I need to validate an email address which has the full name of the person appended to the...
3
by: Smokey Grindel | last post by:
Alright so I have a string... that can be anything like this then have a number like 102.34m, yes there is a m behind it to say "this is money", no I didn't design the spec thats just how data...
2
by: Hillbilly | last post by:
Its not that I hate RegEx its that I have to relearn the same stuff every couple of months because I forget what I last learned. Common eh? So... I'm working on an expression to validate a MIME...
4
by: jeddiki | last post by:
Hi, I am not sure if this can be solved with regex, possibly the string needs to be chopped into words and then stepped through ( but not sure how). Anyway, this is what I have and it is very...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.