Multiple Regex Replacements In C#?

Nov 10, 2010

I am trying create a Regex-based replacement for an article to automatically convert embedded references (many of them) in posts into the appropriate link and title format.

For example, given this:

I have already blogged about this topic ((MY TOPIC ID: "2324" "a number of times")) before. And I have also covered ((MY TOPIC ID: "777" "similar topics")) in the past.

... I want to get this:

I have already blogged about this topic <a href='/post/2324'>a number of times</a> before. And I have also covered <a href='/post/777'>similar topics</a> in the past.

I currently have this:

/* Does not work */
public static string ReplaceArticleTextWithProductLinks(string input)
{
string pattern = "\(\(MY TOPIC ID: \".*?\" \".*?\"\)\)";
string replacement = "<a href='/post/$1'>$2</a>";
return Regex.Replace(input, pattern, replacement);
}

But it seems to return lines that contain <a href='/post/'></a> without appending matches instead of $1 and $2.

Question: What is the easiest way to do convert the string #1 above to string #2 above?

View 1 Replies


Similar Messages:

Regex Pattern / How To Use Multiple Instances

Mar 7, 2011

YARPQ - Yet Another Regex Pattern Question:

What I am trying to figure out is how to use a custom delimiter, single character or set of characters, to wrap a block of text inside a larger body of text and then pull that text out via regex. I have successfully done this with a single instance of wrapped text. If I have multiple instances, it blows up. Here is an example.

This would be my text block with the internally wrapped text:

[Code]....

When my current regex runs on this block, the greedy nature gives me a match starting with my first wrapped block delimeter and ending with my second wrapped block end delimeter. So my regex of @"#Delim(.*)#EndDelim" doesn't work in this instance. I am trying to figure out how to exclude the #EndDelim from my grouping. Simply using ([^#EndDelim]*) doesn't work as it tells regex to ignore the individual characters inside the brackets and not the actual word so this breaks if I have any of those characters inside my wrapped block of text.

I am probably making this harder than it needs to be. Does anyone know of a good solution or how to ignore entire words in regex?

View 3 Replies

What Are Classic Ado's Recordset Replacements

Mar 15, 2011

I've tried to do my homework on this...

I'm migrating a site from classic asp to asp.net 4.0

Before I rework all the code, I wanted to get clarification that the way i think my database code should be updated is fairly correct. (I'm using SQLClient objects)

First, when old code would use forward only recordsets or access single record/value, I'm fairly sure i'd use SqlCommand and SqlDataReader. That is pretty straightforward.

Now, when my old code navigates a recordset (movePrevious, find, etc.)... I believe the way to go is with SqlDataAdapter and DataTable to retrieve the data and DataView to filter/find/navigate the data.

I'n looking to do a quick migration... i don't want to get too bogged down in the new entity framework... so - just trying to stick with similar objects/logic.

I know this is one of those questions that doesn't really have a right/wrong answer... and I think that is part of my problem in finding an answer - there are so many.

View 2 Replies

C# - String Replacements For Word Merge

Mar 31, 2011

using asp.net 4 we do a lot of Word merges at work. rather than using the complicated conditional statements of Word i want to embed my own syntax. something like:

Dear Mr. { select lastname from users where userid = 7 },
Your invoice for this quarter is: ${ select amount from invoices where userid = 7 }.......

ideally, i'd like this to get turned into: string.Format("Dear Mr. {0}, Your invoice for this quarter is: ${1}", sqlEval[0], sqlEval[1]);

View 4 Replies

Web Forms :: Regex - Avoid Replacing Multiple Matches With The Same Value

Feb 12, 2010

I have a dummy html file:

.....<body>
<p><a href="http://www.google.com">link 1</a></p>
<p><a href="http://www.google.com">link 2</a></p>
<p><a href="http://www.google.com">link 3</a></p>
<p> </p>
</body>

I need to add a autoincrement int after the http://www.google.com, though unique for each occurance so the result will be:

<body>
<p><a href="http://www.google.com" 1>link 1</a></p>
<p><a href="http://www.google.com" 2>link 2</a></p>
<p><a href="http://www.google.com" 3>link 3</a></p>
<p> </p>
</body>

Obviously with .replace I get [URL] 3 2 1 > 1 and so on... I am thinking of contructing a two dimensional Array where myArray[0][0] will be the first match like myArray[URL][0] the second element the second match. myArray[URL][1] and so on. So then I would iterate through the second dimension of the array. Will this work? I am thinking that it won't...

View 3 Replies

Regex For Multiple Email Address Using A Specific Domain?

May 6, 2010

i have an requirment where an user can enter multiple email address.

The validation should ensure that he should enter address in the format "something@abc.com".

my existing validation ensures that some text is entered before "@" symbol and "abc.com" is the domain.

@"w+([-+.']w+)*@abc.com$";

I would need my regex to check for the delimiter ";" between two mail id's .

valid: a@abc.com |
a@abc.com;b@abc.com|

invalid: a@abc.comm | a@abc.com,b@abc.com | @abc.com |
a@abc.comb@abc.com|

I've tried many links but ther were of no use and i'd be grateful if someone could help me modify the current one to suit my requirments

View 7 Replies

Using RegEx With LINQ - Insert A Value Into The Regex?

Feb 19, 2010

I'm having a little trouble with using regex in linq. I want to be able to perform a search on values in my database. The goal is to insert a value into the regex and then use that to search a column in a table in my database for that term. I want it to work so that it gets all exact matches and also returns matches in which the search term is a substring of the term in the column.

I was trying to follow this tutorial on msdn, but it doesn't quite fit perfectly with my problem:

[URL]

Code:

[code]....

View 11 Replies

C# - How To Regex For Extenionless URL

Feb 19, 2010

In my asp.net application i have url [URL] to match it i have regex

"~/(.+)" which works perfectly fine. But if i navigate to www.site.com/login.aspx again regex matches with this expression "/(.+)".

In Simple words i want a regex which only match the extenionless url. IF extension is present in the url then do not match it

<RewriterRule>
<LookFor>~/(.+)/(.+)/(.+)/</LookFor>
<SendTo>~/Shop/Item.aspx?cn=&1&it=$2&ft=$3</SendTo>
</RewriterRule>

View 3 Replies

C# - How To Replace Url Value With Regex

Dec 8, 2010

I need a regex or any other solution to replace an id in the middle of a url (not in querystring).
url example - http://localhost:1876/category/6?sortBy=asc&orderBy=Popular

replace - category/6 with category/anotherID -
routing used -
routes.MapRoute(
"categories",
"category/{categoryID}/{categoryName}",
new { controller = "Search", action = "SearchResults", categoryID = "", categoryName = "" }
);

View 1 Replies

Regex.replace [h2] To <h2>?

Aug 25, 2010

I try to replace [h2] to <h2> I use this coding:

string o = "<h2>test</h2>";
o = Regex.Replace(o, "[h2]", "<h2>");
o = Regex.Replace(o, "[/h2]", "</h2>");

But the result after the first regex is:

[<h2><h2>]test[/<h2><h2>]

So that is not what i want.

After the first 1, he should make:

<h2>test[/h2]

Whats wrong?

View 3 Replies

C# - Format Ssn Using Regex

Mar 10, 2011

I need regex in c sharp to display SSN in the format of xxx-xx-6789. i.e 123456789 should be displayed as xxx-xx-6789 in a textfield. The code I am using write now is

string SSN = "123456789";
Regex ssnRegex = new Regex("(?:[0-9]{3})(?:[0-9]{2})(?:[0-9]{4})");
string formattedSSN = ssnRegex.Replace(SSN, "XXX-XX-${last}");

What is correct Reg Expression to mask ssn xxx-xx-6789 ?

View 1 Replies

Two Regex Expressions In One?

Jul 28, 2010

i have the code:

Regex r = new Regex("<tag>([^<]*)</tag>");

but i want to add another set of tags to search between:

Regex r = new Regex("<tag>([^<]*)</tag>|<asd as="b">([^<]*)<asd as=j>");

this does not work.

View 1 Replies

Modifying Existing Regex?

Jan 19, 2010

i m using a regex to not allow certain characters, the below code works fine for certain html tags but not for all for i dont want the user to enter html tags for eg if i enter <nitin/> it allows but i dont want this to happen for other html tags it does not allow

!@#$%^&*()+=[]\';,/{}|":<>? or
!@#$%^&*()+=[]\';,/{}|":<>/>?

View 1 Replies

Regex For Integer Range

Jul 26, 2010

how i can implemnt a regex that cover integer range? means from 0 to 2147483647

View 9 Replies

Regex To Replace Specific Div?

Feb 8, 2010

with a regex that would replace the following. The only thing that would remain the same is the div tags, the id's and classes could change and so could the content.

<div id="nav" class="whatever">Content is whatever</div>

View 4 Replies

C# - Regex Replace - But With A Few Exceptions?

Mar 11, 2010

string lNewHTML = Regex.Replace(lOldHTML, "(word1|word2|word3)", "<a href="page.aspx#$1">$1</a>", RegexOptions.IgnoreCase);

The code works, but I need to include some exceptions to the replace - e.g. I will not replace anything i an img-, li- and a-tag (including link-text and attributes like href and title) but still allow replacements in p-, td- and div-tags.

View 2 Replies

Using RegEx Replace Function In C# ASP?

Feb 18, 2010

How do I solve the problem below? I'm creating a simple content management system, where there is a HTML template with specific markup that denotes where content should be:

<html><head></head><body><!-- #Editable "Body1" --><p>etc etc</p><!-- #Editable "Extra" --></body></html>

Separate from this, there is content in a database field that looks a little like this:

<!-- #BeginEditable "Body1" -->This is Test Text<!-- #EndEditable --><!-- #BeginEditable "Extra" -->This is more test text<!-- #EndEditable -->

As you can guess I need to merge the two, that is, replacing

<!-- #Editable "Body1" -->

with: This is Test Text. I've begun the code here. But I'm having problems using the Regex Replace function that should be located at the very bottom of that For/Each.

//Html Template
string html = "<html><head></head><body><!-- #Editable "Body1" --><p>etc etc</p><!-- #Editable "Extra" --></body></html>";
//Regions that need to be put in the Html Template
string regions = "<!-- #BeginEditable "Body1" -->This is Test Text<!-- #EndEditable --><!-- #BeginEditable "Extra" -->This is more test #EndEditable -->";
//Create a Regex to only extract what's between the 'Body' tag
Regex oRegex = new Regex("<body.*?>(.*?)</body>", RegexOptions.Multiline);
//Get only the 'Body' of the html template
string body = oRegex.Match(html).Groups[1].Value.ToString();
// Regex to find sections inside the 'Body' that need replacing with what's in the string 'regions'
Regex oRegex1 = new Regex("<!-- #Editable "(.*?)"[^>]*>",RegexOptions.Multiline);
MatchCollection matches = oRegex1.Matches(body);
// Locate section titles i.e. Body1, Extra
foreach (Match match in matches)
{
string title = oRegex1.Match(match.ToString()).Groups[1].ToString();
Regex oRegex2 = new Regex("<!-- #BeginEditable "" + title + ""[^>]*>(.*?)<!-- #EndEditable [^>]*>", RegexOptions.Multiline);
//
//
// Replace the 'Body' sections with whats in the 'regions' string cross referencing the titles i.e. Body1, Extra
//
//
//
}

View 5 Replies

C# - Regex To Get Page Number From The Url?

Jul 4, 2010

I am currently using this regex to get the page number from the url:

@"/(d+)$";

Which worked fine for urls like:

/some_category/2
/some_category2/323

My urls now have some additional querystring values (optionally), so the current regex is not working for these cases.

So now I need a regex to support:

/some_category/2
/some_category2/323
/some_category2/323?a=1&b=2
/some_category2/323?&c=123&a=1&b=2

I am using a route with wildcards so I can use the built in asp.net mvc for this.

View 3 Replies

Regex - How To Validate ID Number

Sep 7, 2010

I am new at this so be easy on me... :)

I need to validate if a the ID number that the user typed in my site is a valid ID.

How do I check it?

Do I need to use RegularExpressionValidator?

More over, I also need to validate the credit card number, I found a few RegularExpressions for that in the net but each one is different from the other and I am not sure which one to use.. Does anyone know od a working expression that will suit all credit cards?

View 2 Replies

RegEx For Strong Password?

Mar 6, 2010

I have the following password requirements:

1) Should be 6-15 characters in length
2) Should have atleast one lowercase character
3) Should have atleast one uppercase character
4) Should have atleast one number
5) Should have atleast one special character
6) Should not have spaces

Can anyone suggest me a RegEx for this requirement?

View 4 Replies

MVC :: Routing - How To Get URL Parameters With Regex

May 27, 2010

I would like to be able to type in a URL like [URL] arrive at a Details Page which parsed the "M" and the "123" from the query string and used them as params to query a Table in my Database. I've been reading my @$$ off and I can't find anything close :>

View 3 Replies

Regex To Extract Numbers?

Jul 27, 2010

i'm looking for a regex that does the ffg:

string1 = Test1234

string2 = Test9999

i want the regex to return 1234 and 9999 if the string starts with Test.

View 3 Replies

C# - Replace Dot(.) With Comma(,) Using RegEx?

Aug 4, 2010

I am working on ASP.NET (C#) application. I want to change number decimal figure with comma(,) where i have dot(.) using regular expression.

For example:

Price= 100,00.56

As this international rule of representing numeric values but I Sweden they have different ways for numbers Like

Price= 100.00,56

So i want to change dot(.) into comma(,) and comma(,) into dot(.) using RegEx.

View 7 Replies

C# - Email Validate ( Not Only Regex)?

Jan 9, 2011

I want to validate complete Email address. I am not asking to match using pericular string. but suppose someone enter email id a@bnm.com then first the bnm should be validated and if such domain found then also it should check for such a@bnm.com is available or invalid.???

View 2 Replies

Regex That Matches Newline?

Mar 29, 2010

I have a php function, preg match which i need to convert into c#..It has a regular expression
"/\s+/"....

Its supposed to match the whole string including newline..and in PHP i am getting the whole string...I want to get the result of the whole string match in C#...i have used Regex.match method...But it returns me a blank array..Suppose the string = "title"....Why am i not getting the correct match..?

View 4 Replies







Copyrights 2005-15 www.BigResource.com, All rights reserved