Jump to content

RegEx output to Array


Go to solution Solved by SmOke_N,

Recommended Posts

Hello guys,

I need your help again .. I need just a little push it the right direction :)

This is what I am trying to do:

I have a text file called "log.txt" that has an X amount of lines. The lines in the file look like this: 

 
I have a regular expression that goes through the file and searches for a line that has the "page/{number}" in it. 
I want to output this to an array. Some of the lines in the "log.txt" file might not contain the "page/{number}" so I want to skip them and have only the ones returned by my regular expression in the array.
 
 
This is what I have until now, and for some reason it is not outputting to an array. I have tried the regular expression in this website https://regex101.com/ and I know it is working because it is returning the values that I want and omitting the ones that I don't want, but how the :evil:  do I put all that into an array?
#include <Array.au3>
#include <File.au3>
#include <MsgBoxConstants.au3>
#include <StringConstants.au3>

Local $LogFile = ("log.txt")                                                                            ;Set a variable for the filename
FileOpen($LogFile, 0)                                                                                   ;Open the file with read-only
Local $aArray_RegEx[] = [0]                                                                             ;Create empty array NOT SURE ABOUT THIS ONE ????
Local $aArray_RegEx = StringRegExp($LogFile, "(?is).+page/\b\d{0,}\b" ,$STR_REGEXPARRAYFULLMATCH)       ;search an array for link containing "page/{number} and output to array
For $i = 0 To UBound($aArray_RegEx) - 1                                                                 ;loop through the array and give me a message
    MsgBox($MB_SYSTEMMODAL, "RegExp Test with Option 2 - " & $i, $aArray_RegEx[$i])
Next 

Your help is much appreciated guys. I know it's something so easy ....

 

Link to comment
Share on other sites

  • Moderators
  • Solution

(?ism).+?/page/d+

Edit:

BTW, you have to "Read" the file with FileRead(), FileOpen does nothing but return a handle

So:

#include <Array.au3>
Local $LogFile = "log.txt"
Local $sRead = FileRead($LogFile)
Local $aArray_RegEx = StringRegExp($sRead, "(?ism).+?/page/\d+" ,$STR_REGEXPARRAYFULLMATCH) 
_ArrayDisplay($aArray_RegEx)
Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

SmOke_N thank you very much.

One more question: Why the regular expression search only brings me the first line and it doesn't search the rest of the file? Wasn't the (?m) supposed to search the whole file?

Figured it out ...

It's because the "$STR_REGEXPARRAYGLOBALMATCH" should be used instead of "$STR_REGEXPARRAYFULLMATCH"

 

$STR_REGEXPARRAYGLOBALMATCH finds all the matching instances while, $STR_REGEXPARRAYFULLMATCH will find only the 1st match and return the captured group.

Link to comment
Share on other sites

Sorry I am revving and older thread, but is there a way to use variables in your regular expression search?

For example if I have 

$VAR = 100

Can I do this?

StringRegExp("TEST 100", "(?ism)($VAR)")

I want to use variable in my search because I might need to search for different string every now and then. I am getting the $VAR variable from another file, but how do I search for it?

Thanks guys!

Link to comment
Share on other sites

Sorry I am revving and older thread, but is there a way to use variables in your regular expression search?

 

For example if I have 

$VAR = 100

 

Can I do this?

 

StringRegExp("TEST 100", "(?ism)($VAR)")

 

I want to use variable in my search because I might need to search for different string every now and then. I am getting the $VAR variable from another file, but how do I search for it?

 

Thanks guys!

Use "&" to concatenates/joins two strings.

Local $VAR = 100

; The "\b" word boundaries prevent 10 or 0 being matched when the $VAR is 100.
If StringRegExp("TEST 100", "(?ism)\b" & $VAR & "\b") Then
    MsgBox(0, "Results", $VAR & " is present in the subject test string")
Else
    MsgBox(0, "Results", $VAR & " is not present in the subject test string")
EndIf
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...