Pego esta macro de extración para el visual studio que he creado.
Sirve para extraer de un documento todo el texto que cumpla una expresión regular dada.
La uso para obtener de un aspx todos los ids de los controles.
La maro, busca en el documento las concidencias con la expresión regular y pega en la ventana de Output el texto correspondiente al primer grupo dentro de la expresión regular.
Expresiones regulares propuestas:
Si queremos obtener todos los controles que tengan id: id=”([a-zA-Z0-9_]+)”
Para los controles con id que comienze por txt: id=”(txt[a-zA-Z0-9_]+)”
Que comienze por lbl : id=”(lbl[a-zA-Z0-9_]+)”
Sub ExtraerIDS() ' Obtener referencia a la ventana de Output Dim win As Window = _ DTE.Windows.Item(EnvDTE.Constants.vsWindowKindOutput) Dim ow As OutputWindow = win.Object Dim owPane As OutputWindowPane owPane = ow.OutputWindowPanes.Item("General") ' Activar y limpiar la ventana de Output owPane.Activate() owPane.Clear() ' Activar la ventana del documento activo DTE.ActiveDocument().Activate() ' Mover el cursor al principio del documento DTE.ActiveDocument.Selection.StartOfDocument() ' Obtener Parámetros de búsqueda Dim searchRegExpr As String = "id=""([a-zA-Z0-9_]+)""" searchRegExpr = InputBox("Expresión de Búsqueda", "Extraer IDS", searchRegExpr) ' Establcer parámetros de búsqueda DTE.Find.FindWhat = searchRegExpr DTE.Find.Target = vsFindTarget.vsFindTargetCurrentDocument DTE.Find.MatchCase = False DTE.Find.MatchWholeWord = False DTE.Find.MatchInHiddenText = True DTE.Find.PatternSyntax = vsFindPatternSyntax.vsFindPatternSyntaxRegExpr DTE.Find.Action = vsFindAction.vsFindActionFind DTE.Find.Backwards = False ' Ultima línea encontrada. La usamos para no buscar varias veces por el documento ' ya que la búsqueda no se detiene al final, sino que vuelve al principio ' y sigue buscando Dim lastFoundLine As Integer = -1 ' Inicializar expresión regular Dim re As New System.Text.RegularExpressions.Regex(searchRegExpr, Text.RegularExpressions.RegexOptions.IgnoreCase) ' Bucle de Búsqueda While DTE.Find.Execute() = vsFindResult.vsFindResultFound And DTE.ActiveDocument.Selection.TopPoint.Line >= lastFoundLine ' Texto que mostraremos en el panel Output Dim textToShow As String = DTE.ActiveDocument.Selection.Text ' Buscamos los grupos dentro de la expresión regular Dim match As Text.RegularExpressions.Match = (re.Match(textToShow, searchRegExpr, Text.RegularExpressions.RegexOptions.IgnoreCase)) ' Si hay algún grupo dentro de la expresión regular If match.Groups.Count > 0 Then textToShow = match.Groups(1).Value Else textToShow = match.Groups(0).Value End If ' Si hay algo que mostrar If textToShow.Length > 0 Then owPane.OutputString(textToShow & vbCrLf) End If ' Comprobamos que hayamos llegado al final del documento. lastFoundLine = DTE.ActiveDocument.Selection.TopPoint.Line End While MsgBox("End!") End Sub
Crossposted from crisfervil.com