Regex "gensym?"

  • Thread starter Thread starter Bill Cohagan
  • Start date Start date
B

Bill Cohagan

I have an app where I'll be using Regular Expression substitution to make
some changes in an HTML file. In particular I'm adding STYLE attributes to
selected TEXTAREA and INPUT elements. In addition to this however I need to
add an ID attribute which of course must be unique.

So, the problem is, how can I create the ID values to be used during the RE
substitution? The only constraint is that the ID values must be unique and
of course must satisfy the HTML syntax for such values. A GUID would work
nicely for instance. My impression is that this can't be done using just RE
substitution, although obviously it can be done if the RE is being run under
control of a .Net application that gets control back after each hit. I'd
prefer not to do that however.

Does the .Net implementation of RE provide any mechanism that I might use to
accomplish this?

TIA,
Bill

PS - "gensym" is from the Lisp function that does exactly what I need.
 
Hello Bill,

Thanks for your post. I reviewed your description carefully, I think more
information is needed before moving forward:

By "Regular Expression substitution", do you mean the
RegularExpressionValidator control in .NET?
Do you want to create TextBoxes each has a unique ID to be used in RE
substitution?
Would you please tell me the detailed information of what you are trying to
do?

In addition, you can create a GUID by the tool Guidgen.exe, or call
Guid.NewGuid to get a GUID programmatically.

Guid.NewGuid Method
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
frlrfsystemguidclassnewguidtopic.asp

I look forward to your response.

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! -- www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Bill said:
I have an app where I'll be using Regular Expression substitution to make
some changes in an HTML file. In particular I'm adding STYLE attributes to
selected TEXTAREA and INPUT elements. In addition to this however I need to
add an ID attribute which of course must be unique.

So, the problem is, how can I create the ID values to be used during the RE
substitution? The only constraint is that the ID values must be unique and
of course must satisfy the HTML syntax for such values. A GUID would work
nicely for instance. My impression is that this can't be done using just RE
substitution, although obviously it can be done if the RE is being run under
control of a .Net application that gets control back after each hit. I'd
prefer not to do that however.

Does the .Net implementation of RE provide any mechanism that I might use to
accomplish this?

TIA,
Bill

PS - "gensym" is from the Lisp function that does exactly what I need.

The framework's Regex.Replace() method has overloads that include a
MatchEvaluator delegate.

This delegate will be called to evaluate each match, and return the
replacement string. All you need to do it create a MatchEvaluator
delegate that returns:

"id" + Guid.NewGuid().ToString("N")

or something similar.
 
Mikeb
Thanks for the response. I was hoping for something within the Regex
language itself, but your suggestion provides a good fallback solution.

Regards,
Bill

mikeb said:
Bill Cohagan wrote:
work
 
Back
Top