472,794 Members | 3,334 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,794 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 1611
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...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.