Jump to content

How do I get the output of the matching pattern?

Recommended Posts

 Is there a way to output the regex matches into a file?

I have a script to compare two files and check for regex matches.

I want to output the matching regex of 'testexample.txt' to another file.

#include <MsgBoxConstants.au3>
#include <Array.au3>

$Read = FileReadToArray("C:\Users\admin\Documents\testexample.txt")
$Dictionary = FileReadToArray("C:\Users\admin\Documents\example.txt")

For $p = 0 To UBound($Dictionary) - 1 Step 1
    $pattern = $Dictionary[$p]
        For $i = 0 To UBound($Read) - 1 Step 1
        $regex = $Read[$i]

            If StringRegExp($regex, $pattern, 0) Then
                MsgBox(0, "ResultsPass", "The string is in the file, highlighted strings: " )
                MsgBox(0, "ResultsFail", "The string isn't in the file.")


Share this post

Link to post
Share on other sites

It sounds like you're looking for just the matching section to be written to the file, if so, check out the help page for StringRegExp and read up on the flag parameter.

Edit: I would guess you're looking for Flag 3, but you can use this to check... I wrote this because I couldn't understand the what the flags did. Hope it helps :)

#include <Array.au3>


Func RegExpExample()

    Local $sTest = '<test>a</test> <test>b</test> <test>c</Test>'
    Local $sPattern = '(?i)<test>(.*?)</test>'
    Local $vResult

    For $iFlag = 0 To 4
        $vResult = StringRegExp($sTest, $sPattern, $iFlag)
        If @error Then
            ConsoleWrite("!    StringRegExp Error: " & @error & @CRLF & @TAB & "  Flag: " & $iFlag & @CRLF)
        ElseIf IsArray($vResult) Then
            If $iFlag = 4 Then
                For $i=0 To UBound($vResult) - 1
                    If IsArray($vResult[$i]) Then
                        _ArrayDisplay($vResult[$i], "Flag " & $iFlag & " Part " & $i & "- StringRegExp Result")
                _ArrayDisplay($vResult, "Flag " & $iFlag & " - StringRegExp Result")
            MsgBox(0, "Flag " & $iFlag & " StringRegExp Result", $vResult)
Edited by seadoggie01

All my code provided is Public Domain... but it may not work. ;) Use it, change it, break it, whatever you want.


My Humble Contributions:
Personal Function Documentation - A personal HelpFile for your functions
Acro.au3 UDF - Automating Acrobat Pro
ToDo Finder - Find #ToDo: lines in your scripts

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By seadoggie01
      I'm trying to capture everything after a "#ToDo" in my scripts. I got that like this:
      (?i)[^\v]*#todo(.*) But then I thought it would be nice to use underscores to continue the ToDo... kind of like this:
      #ToDo: This is a really long explanation about something _ # that is very in-depth and needs to take up a lot of _ # space in a ToDo comment Global $variables = "Bad" I can't seem to capture everything... and maybe I'm trying to do too much with Regex... I keep trying variations of this:
      Condensed Version: (?im)[^\v]*#todo(?:([^\v]*)_\s*)*#([^\v]*) Expanded with comments (?ixm)(?# Ignore case, ignore newlines in Regex, use multiline option)# [^\v]*(?# Match leading space/s)# \#todo(?# Match the #todo)# (?:([^\v]*)_\s*)*(?# Match lines ending with _)# \#([^\v]*)(?# Last line only, no _'s)# I never seem to be able to build an array well with Regex... I saw something once about not being able to capture repeated patterns, and I think that's my issue
    • By genius257
      Inspired by PHP's preg_split.
      Split string by a regular expression.
      Also supports the same flags as the PHP equivalent.
      #include "StringRegExpSplit.au3" StringRegExpSplit('splitCamelCaseWords', '(?<=\w)(?=[A-Z])') ; ['split', 'Camel', 'Case', 'Words']  
    • By jmp
      i am trying to get number from string using this code :
      #include <IE.au3> $oIE = _IEAttach ("Edu.corner") Local $aName = "Student name & Code:", $iaName = "0" Local $oTds = _IETagNameGetCollection($oIE, "td") For $oTd In $oTds If $oTd.InnerText = $aName Then $iaName = $oTd.NextElementSibling.InnerText $iGet = StringRegExpReplace($iaName, "\D", "") EndIf Next MsgBox(0, "", $iGet) it was get number like 52503058
      But, I want to get only student code 5250. (Different student have different code, sometime its 3 digits, Sometime 4)

    • By RAMzor
      Hi guys I need your help.
      I have string like this : "TDM111A5,      RCT222Y5/ 7  ; FDT444E4 /8 , ABC222R5"
      I need find a coma or semicolon and delete white spaces before and after them
      The output should be a string and/or array 
      String : "TDM111A5,RCT222Y5/ 7;FDT444E4 /8,ABC222R5"
      RCT222Y5/ 7
      FDT444E4 /8
  • Create New...