473,569 Members | 2,762 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.rp l"
Dim regx1 As New RegExp
Dim m As Match, mc As MatchCollection , sm As SubMatches
Dim sText As String
Dim fs As New FileSystemObjec t
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(st ring filename)
{
string alltext = new StreamReader
(@"D:\Johnw\Dat a\RPLs\V2 RPLs\alldata.rp l").ReadToEnd() ;
Regex re = new Regex("^[0-9]{4}.{74}01$", RegexOptions.Mu ltiline);

MatchCollection mc = re.Matches(allt ext);
Console.WriteLi ne("Found " + mc.Count.ToStri ng() + " matches");
if ( mc.Count == 0 )
{
return "";
}
return filename;
}

--
John Wood a.k.a Mortimer Schnurd
http://www.loosemarbles.com
Nov 15 '05 #1
2 1646
On Mon, 01 Mar 2004 17:46:43 -0500, in msg
<5n************ *************** *****@4ax.com>, Mortimer Schnurd
<fu**********@h otsmail.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.rp l"
Dim regx1 As New RegExp
Dim m As Match, mc As MatchCollection , sm As SubMatches
Dim sText As String
Dim fs As New FileSystemObjec t
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(st ring filename)
{
string alltext = new StreamReader
(@"D:\Johnw\Dat a\RPLs\V2 RPLs\alldata.rp l").ReadToEnd() ;
Regex re = new Regex("^[0-9]{4}.{74}01$", RegexOptions.Mu ltiline);

MatchCollection mc = re.Matches(allt ext);
Console.WriteLi ne("Found " + mc.Count.ToStri ng() + " 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**********@h otsmail.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.rp l"
Dim regx1 As New RegExp
Dim m As Match, mc As MatchCollection , sm As SubMatches
Dim sText As String
Dim fs As New FileSystemObjec t
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(st ring filename)
{
string alltext = new StreamReader
(@"D:\Johnw\Dat a\RPLs\V2 RPLs\alldata.rp l").ReadToEnd() ;
Regex re = new Regex("^[0-9]{4}.{74}01$", RegexOptions.Mu ltiline);

MatchCollection mc = re.Matches(allt ext);
Console.WriteLi ne("Found " + mc.Count.ToStri ng() + " 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
10902
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 empty strings in the resulting string array.
7
2571
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 slash then some numbers. For some reason I am not getting that. It won't work at all in 2.0
2
1819
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' well that 45.1234 should not have an m there... sure this works on data views filters but not on SQL Server which is what the criteria needs to be...
0
1828
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 RegExp regEx.Pattern = "href=\""(xlink:.*?)\""" output = regEx.Replace(output,"href="&chr(34) &
0
1001
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 and succeeded by a non-alphanumeric character only”. This time I need to NOT allow a keyword to be matched if it is preceded or succeeded by a $...
6
1495
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 beginning of it. Here is an example of what I’m trying to validate. “firstname lastname” <username@domain.com> I need to enforce this format so the web...
3
2522
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 comes in... well I had a regex that replaces all those m's in a string where there is a number before it with no m... so its just the string with...
2
1232
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 Type and I have a fairly decent pattern set up but it needs some work... <%-- MIME Type match: type/name match: type/name5 match: type/x-name
4
1478
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 close to what I want.: preg_match_all("#((?:\b\w{1,20}\b\s+){2})#", $data, $matches)
0
7694
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7609
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7921
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8118
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7964
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5504
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3636
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1208
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
936
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.