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?
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?
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.
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]);
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...
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:
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 = "" } );
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
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 !@#$%^&*()+=[]\';,/{}|":<>/>?
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>
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.
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:
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 // // // }
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?
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?
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 :>
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.???
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..?