Introduce DataBase,Asp.net,JavaScript,Xml,Html,Css,Sql,Php,ASP.NET Controls,AJAX,Tools,HTML,CSS,JavaScript,Open Source Project,WPF,.Net Framework,Linq
Top Recommended Hosting

Inserting Text into a Regular Expression

by the3factory 4/27/2008 6:30:00 PM
Regular Expressions Inserting Text into a Regular Expression
Question:

With regular expressions you can have groups that require a match but aren't captured.  Is there a way to do the inverse: have a group that is captured but does not have to match?  That way a regular expression could dictate bits of text to insert into the result all within a single call to Match().

 

Thank you for your time,

Chris Johnson

Answer1:
What are you trying to achieve? Give us a hard example with text and what you expect to match and not to match.



www.OmegaCoder.Com
Answer2:

I have a program where the user enters regular expressions to parse websites.  For example, if the website has something like:

temperature=55

I would use @"temperature=(\d+)" to grab that value.  But what if the user didn't want the output to be:

55

but wanted the output to be:

55°

So is there any way to insert text into the output through the regular expression?  For example, let's say anything between two parentheses with a pound sign (#text) would be inserted into the output but not matched in the regular expression so that the syntax would look like:

@"temperature=(\d+)(#°)"

and output:

55°

Thank you,
Chris Johnson

 

Answer3:
 ChrisAtSilentOrb wrote:

I have a program where the user enters regular expressions to parse websites.  For example, if the website has something like:

temperature=55

I would use @"temperature=(\d+)" to grab that value.  But what if the user didn't want the output to be:

55

but wanted the output to be:

55°

So is there any way to insert text into the output through the regular expression?  For example, let's say anything between two parentheses with a pound sign (#text) would be inserted into the output but not matched in the regular expression so that the syntax would look like:

@"temperature=(\d+)(#°)"

and output:

55°

Thank you,
Chris Johnson

 

 

I think you can do it via a Regex.Replece method. You will have to write a delegate MatchEvaluator and declare and reference it. The delegate will do anything you want to do.

 

ms-help://MS.VSCC.v90/MS.MSDNQTR.v90.en/fxref_system/html/15f2c767-4638-3fe5-0508-fc55485ee408.htm




AlexB
Answer4:
Alex is right, either you have to design the regular expression and use the replace regex or one can use the MatchEvaluator that only .Net provides as a programmatic way of altering a match before it is consumed by the caller. I have an example of the MatchEvaluator on my blog entitled, .Net Regex MatchEvaluator.



www.OmegaCoder.Com

Related posts

Sign up for PayPal and start accepting credit card payments instantly.


Powered by BlogEngine.NET 1.2.0.0