473,404 Members | 2,137 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,404 software developers and data experts.

Inheriting Regex

Rob
I have built a class called DTLineScrub that inherits Regex.
All works well with the code below until I try to do a replace on a string.
It then tells me that the object was not instantiated.

Anyone have an Idea what's going on?

Much Appreciated!

Rob

string myTEST = "ABE'\"TH|IS";

DTLineScrub dtls = new DTLineScrub();
dtls.CustomRegularExpression = @"[\cA-cZ]";

MessageBox.Show( dtls.ToString() );

myTEST = dtls.Replace(myTEST," ");

MessageBox.Show( myTEST );

Aug 10 '06 #1
6 1667
Rob <Ro*@discussions.microsoft.comwrote:
I have built a class called DTLineScrub that inherits Regex.
All works well with the code below until I try to do a replace on a string.
It then tells me that the object was not instantiated.

Anyone have an Idea what's going on?
Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Aug 10 '06 #2
Rob
Thanks Jon for looking at this for me. I have created a console app that will
show the issue in total. If you have any questions, please feel free to
contact me directly at ro*@tennsoft.com.

You can just slap this into an empty page and it will compile and run to the
error.

Cheers!

Rob

--------------------------------- CODE
-----------------------------------------------------

/* This code results in the following error:
* An unhandled exception of type 'System.NullReferenceException' occurred
in system.dll
* Additional information: Object reference not set to an instance of an
object.*/

using System;
using System.Text;
using System.Text.RegularExpressions;

namespace SampleCode
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
//creation of the regularexpression object
static regx lineScrub = new regx();
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
//Create the line to be scrubbed
string line = "This pipe| should be taken out and so should this quote.\"";

//Action of the object - error occurs on this line
line = lineScrub.Replace(line, " ");

//Never gets here.
Console.WriteLine( line );
}
}

//regularexpression object created that inherits the
System.Text.RegularExpressions.Regex object.
class regx : Regex
{
public regx()
{
this.pattern = "\\\"\\|";
}
}
}

-------------------------------- End Code
-------------------------------------------------

"Jon Skeet [C# MVP]" wrote:
Rob <Ro*@discussions.microsoft.comwrote:
I have built a class called DTLineScrub that inherits Regex.
All works well with the code below until I try to do a replace on a string.
It then tells me that the object was not instantiated.

Anyone have an Idea what's going on?

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Aug 11 '06 #3
Right;

First - your regex is wrong; it currently says "match quote followed by
pipe".

Second - the protected .pattern *tells* you (intellisense) that it is
intended for use in CompileToAssembly mode - which you aren't using, so it
doesn't apply; you need to use the ctor usage; I changed this to:

class regx : Regex {
public regx() : base(@"[\""\|]") {}
}

Third - I'd challenge the need to subclass here; it would be far less
complex to just declare

Regex lineScrub = new Regex(@"[\""\|]");

(Perhaps stored somewhere handy with compile-to-assembly if it is going to
be re-used much)

Marc
Aug 11 '06 #4
Note - going back to your original issue... if I really wanted this type of
API, I would wrap a regex instead... see below... but to be honest, Regex et
al are so common that (without siginificant extra functionity) I'd need
convincing to use it myself...

public class MyWrapper {
private Regex regex;
private string exp;
public string Pattern {
get {return exp;}
set {if(exp!=value) {
// do this first so if it barfs we haven't changed exp
// (and maybe compile too...)
regex = new Regex(value);
exp = value;
}}
public blah Replace(blah) {}
public blah IsMatch(blah) {}
// blah
}
Aug 11 '06 #5
Rob
Thanks Marc,

The purpose of the class is to be able to add properties that would automate
selections of common regular expressions without the developer needing write
the expressions directly. The idea was to inherit the Regex object into the
class and add the properties to change the pattern.

I see that the only way to do this, is to set the class to a new Regex for
each new addition to the expression. Knowing now that the expression must be
passed in at the time the Regex Object is instantiated tells me that I need
to go the rout you noted in this last reply.

Thanks.

Rob

"Marc Gravell" wrote:
Note - going back to your original issue... if I really wanted this type of
API, I would wrap a regex instead... see below... but to be honest, Regex et
al are so common that (without siginificant extra functionity) I'd need
convincing to use it myself...

public class MyWrapper {
private Regex regex;
private string exp;
public string Pattern {
get {return exp;}
set {if(exp!=value) {
// do this first so if it barfs we haven't changed exp
// (and maybe compile too...)
regex = new Regex(value);
exp = value;
}}
public blah Replace(blah) {}
public blah IsMatch(blah) {}
// blah
}
Aug 11 '06 #6
I must admit though, the Regex class was /very/ misleading here...

In the MSDN docs, it claims to be immutable (although it doesn't carry the
immutable attribute) - and as you found, it exposes these protected fields
[aside: why fields? eugh], which makes it look as though you are allowed to
manipulate it in this way (although MSDN does report these fields as not
intended for user consumption). A very misleading implementation, so your
attempted route was perfectly reasonable in the "it looks like it should
work" sense.

I think you just got bit by Redmond ;-p

Best of luck with your wrapper,

Marc
Aug 11 '06 #7

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

Similar topics

3
by: Mike | last post by:
Hello, I would like to write a string-like class that will only allow certain string values, (for example, specified with a regular expression). I'd still like it to behave like a string, I...
9
by: Tim Conner | last post by:
Is there a way to write a faster function ? public static bool IsNumber( char Value ) { if (Regex.IsMatch( Value.ToString(), @"^+$" )) { return true; } else return false; }
20
by: jeevankodali | last post by:
Hi I have an .Net application which processes thousands of Xml nodes each day and for each node I am using around 30-40 Regex matches to see if they satisfy some conditions are not. These Regex...
8
by: Anthony Williams | last post by:
Morning all, I'm having a wee problem with a project I'm working on at the moment. I'm leading my company into producing a website, based upon Web Standards, which will be created using XHTML...
2
by: Charles Law | last post by:
I want a set of controls that all have a border, like a group box. I thought I would create a base control containing just a group box from which my set of controls could inherit. Assuming that...
6
by: Extremest | last post by:
I have a huge regex setup going on. If I don't do each one by itself instead of all in one it won't work for. Also would like to know if there is a faster way tried to use string.replace with all...
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...
3
by: aspineux | last post by:
My goal is to write a parser for these imaginary string from the SMTP protocol, regarding RFC 821 and 1869. I'm a little flexible with the BNF from these RFC :-) Any comment ? tests= def...
15
by: morleyc | last post by:
Hi, i would like to remove a number of characters from my string (\t \r \n which are throughout the string), i know regex can do this but i have no idea how. Any pointers much appreciated. Chris
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.