Friday, January 9, 2015

VB.NET - Regular Expression to get all HREF's out of a String

This function will accept a string and return an arraylist of all the HREF's that were inside:

    ''' <summary>
    ''' Using regular expression to get HREF out of string
    '''
    ''' </summary>
    ''' <param name="inputString"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function GetHtmlTags(inputString As String) As ArrayList
        Dim m As Match
        Dim HRefPattern As String = "<img[^>]+src\\s*=\\s*['\""]([^'\""]+)['\""][^>]*>"
        Dim arrlist As New ArrayList
        Try
            'Using regular expression retrieve all HREFs from the datacontent string
            m = Regex.Match(inputString, HRefPattern, RegexOptions.IgnoreCase Or RegexOptions.Compiled)
            Do While m.Success
                'check to see if the link already exists in the arraylist before adding it again
                If arrlist.Contains(m.Groups(1).ToString()) = False Then
                    arrlist.Add(m.Groups(1).ToString())
                End If

                m = m.NextMatch()
            Loop

            Return arrlist

        Catch ex As Exception
            Throw ex
        End Try
    End Function



VB.NET - Write Text File Using StreamWriter

Imports System.IO

 Dim strDirectory As String = "C:\temp324\"

        If Directory.Exists(strDirectory) = False Then
            Directory.CreateDirectory(strDirectory)
        End If


        Using objWriter As StreamWriter = New StreamWriter(strDirectory & "\links.txt", False)
            objWriter.Write(txtText.Text)
        End Using

Tuesday, August 12, 2014

VB.NET - Find Differences Between 2 Arrays

To get an array of common items between 2 arrays, use the .Except method off of the first array.

i.e.:

Dim strDifferences As String()
Dim strUserGroups As String()
Dim strOrgGroups As String()

strDifferences= strUserGroups.Except(strOrgGroups).ToArray()

VB.NET - Find Common Items Between 2 Arrays

To get an array of common items between 2 arrays, use the .Intersect method off of the first array.

i.e.:

Dim strSimilarities As String()
Dim strUserGroups As String()
Dim strOrgGroups As String()

strSimilarities = strUserGroups.Intersect(strOrgGroups).ToArray()

Tuesday, July 15, 2014

SQL - Update Statement from Join

UPDATE    p
SET       p.phone_ext = d.phone
FROM      people p
JOIN      directory d
ON        p.last_name = d.last_name


Friday, December 27, 2013

ASP.NET - Add Validator to Dynamically Created Control

Dim objValidator As New RegularExpressionValidator()
objValidator.ControlToValidate = txtItem.ClientID
objValidator.ValidationExpression = "[0-9]"
objValidator.Text = "*"
objValidator.ErrorMessage = "Must be in correct format"

objValidator.SetFocusOnError = True


phPlaceholder.Controls.Add(objValidator)