Showing posts with label Regular Expression. Show all posts
Showing posts with label Regular Expression. Show all posts

Add static items(more than 100) in List in less than 10 Sec using Visual Studio

Sometime the task is done quickly using regular expression. "Find & Replace" with "Regular Expression" !!! What's Combination Amazing!!! and can save our time to do huge boring tasks in few seconds.


Generally we are given an excel sheet and said that this column should be appear in drop down list and these are static then How can we insert items quickly
1. Copy Excel Sheet Column which is to be appeared in dropdownlist or Items from text file and Paste in source code file.
2. Select all items.
3. In Visual Studio -> Edit -> Find and Replace and put following thing
Find What: {.+}
Look in: Selection
Use: Regular Expression
Replace With:
<li>\1</li> (HTML List)
<option>\1</option>(HTML Drop Down)
<asp:ListItem>\1</asp:ListItem> (ASP.NET Listbox and DropDown Items)
4. Add Start tag and end tag of control before and after selection.


Don’t forget to say Thanks If it helps.

Remove HTML Tags from the string

Some times we require to remove a particular tag from string. Suppose you need only content, no need to HTML tag present in string. then call following method, give tag name which is to be removed from html string.

Private Shared Function StripHtmlTag(ByVal tagName As String, ByVal html As String) As String
Dim reg As New Regex(String.Format("<{0}[^>]*?>[\w|\t|\r|\W]*?</{0}>", tagName), RegexOptions.Singleline Or RegexOptions.IgnoreCase)
Dim regt As New Regex("\n")
Dim m As Match = reg.Match(html)
While m.Success
html = html.Replace(m.ToString(), New String(Chr(10), regt.Matches(m.ToString()).Count))
m = m.NextMatch()
End While
Return html
End Function

For example, you have string "<div> This is test</div>" Now you have to remove div tag from this string then call above method pass div tag as tagname and string as html. then it yields " This is test ".

Extract Links from the string

I saw a lot of methods or regular expressions to extract all links from source of web page. but problem is that they use anchor href attribute. what would happen if links are in javascript. So I changed this criteria and search links using extensions. Here is the code


Dim mc As MatchCollection = Regex.Matches(stringObject, "(?:\b[A-Z0-9_/.:]+\.(aspx|htm|html|php|asp|js|ascx)(\S+=\S+\"")?\b)|(?:<a[^>]*?>)", RegexOptions.IgnoreCase Or RegexOptions.ExplicitCapture)