Jump to content

Recommended Posts

Hi Programmers,

I have worked with SRE before and I know it's a really powerful tool to get extract exactly what you want from text. However it's a little tricky to use sometimes with a lot of syntax to memorize. I'm having a problem and I know it should work, but I just can't get it to work.

Here is my text:

:ALONEI~2
cd ALONEI~2
AITD2CRK.COM
goto end

:ALONEI~3
cd ALONEI~3
AITD3.EXE
goto end

:ARKANO~1
cd ARKANO~1
start.bat
goto end

:Ascend
cd Ascend
ASCEND.EXE
goto end

What I would like to do is get the text between a label like "Alonel~3" and the line "goto end" below it.

Here is what I have to work with:

1. I will always know the name of the label

2. I will NOT know how many lines is between the label and the line "goto end"

The SRE I have built so far is this, "(?:\:ARKANO~1)(?:\s*)(.*)(?:\s*)(.*)" ;note, the \s must also have a * because I've found without it, it won't work. Maybe the file uses two different linefeeds in one.

Which will give me an array of

0 => cd ARKANO~1

1 => start.bat

Now that's good, but the thing is I don't know how many lines will be inbetween, so I'd like to make an unlimited repeating set Something like this, but one that has the proper syntax and works.

Here are a few tries and fails I've made:

"(?:\:ARKANO~1)[?:\s.]*(?:goto)" ; also note I don't want to capture the line feeds

"(?:\:ARKANO~1)((?:\s)|(.))*(?:goto)"

"(?:\:ARKANO~1)((?:\s*)|(.*))(?:goto)"

"(?:\:ARKANO~1)((?:\s)|(.)){1,}(?:goto)"

Basically I need to know how to put special characters into a set and make it repeating. I want to not capture the \s line feeds, but capture anything else and continue until I hit the "goto" text. I've been at this for a few hours so any help is appreciated.

Link to comment
Share on other sites

Have you looked at the code used in _StringBetween()? It might be helpful. Just pasting that sample text you posted into a file, I tested it and they all came out ok.

#include <string.au3>
$read = FileRead('text.txt')
$sBetween = _StringBetween($read, ':ARKANO~1', 'goto end')
if IsArray($sBetween) Then MsgBox(0, '', $sBetween[0])
Edited by Beege
Link to comment
Share on other sites

Hi Beege,

I have seen that actually, it one of the alternatives I might end up using. However, I really would like to know how to use SRE properly because I often use it and there are other problems I will come across that stringbetween won't beable to solve. I know it's more complicated, and though string between works, I would like to know how to do it the SRE way.

It's not the first time I've come across this particular problem actually. In a different scenario I wanted to beable to use special characters in a set.

Link to comment
Share on other sites

Here is the problem written in code. Incase anybody else wants to try. Though, I like to use the StringRegExpGUI included in the example files of Autoit.

#include <Array.au3>

$searchString = "SRE Syntax Here"

$text = "" & _
  @CRLF & _
  ":ARKANO~1" & @CRLF & _
  "cd ARKANO~1" & @CRLF & _
  "start.bat" & @CRLF & _
  "goto end" & @CRLF & _
  @CRLF & _
  @CRLF & _
  ":Ascend" & @CRLF & _
  "cd Ascend" & @CRLF & _
  "ASCEND.EXE" & @CRLF & _
  "goto end" & @CRLF & _
  @CRLF & _
  @CRLF & _
  ":BEETLE~1" & @CRLF & _
  "cd BEETLE~1" & @CRLF & _
  "BJ.EXE" & @CRLF & _
  "goto end"
$asResult = StringRegExp($text, $searchString, 2)
If @error == 0 Then
_ArrayDisplay($asResult)
EndIf
Link to comment
Share on other sites

Well _Stringbetween() uses SRE. Thats why I said it might be helpful to look at the code. The pattern it generated was:

(?s)(?i):ARKANO~1(.*?)goto end
Link to comment
Share on other sites

The patten I posted above seems to work fine unless I'm missing something? :)

#include <Array.au3>
$searchString = "(?s)(?i):ARKANO~1(.*?)goto end"

$text = "" & _
  @CRLF & _
  ":ARKANO~1" & @CRLF & _
  "cd ARKANO~1" & @CRLF & _
  "start.bat" & @CRLF & _
  "goto end" & @CRLF & _
  @CRLF & _
  @CRLF & _
  ":Ascend" & @CRLF & _
  "cd Ascend" & @CRLF & _
  "ASCEND.EXE" & @CRLF & _
  "goto end" & @CRLF & _
  @CRLF & _
  @CRLF & _
  ":BEETLE~1" & @CRLF & _
  "cd BEETLE~1" & @CRLF & _
  "BJ.EXE" & @CRLF & _
  "goto end"


$asResult = StringRegExp($text, $searchString, 3)
If not @error Then MsgBox(0,'',$asResult[0])

edit: Change the SRE option back to 2 if you want to include the original search items

Edited by Beege
Link to comment
Share on other sites

Not postive because I'm definaitly not a SRE guru but I think the or operator might work. ex: s|H

edit: Just to clarify I did not mean use that in a set. Thats an alternative. The doc specifically says special characters do not retain their special meanings inside a set..

Edited by Beege
Link to comment
Share on other sites

For anyone interested, check this very good tutorial about REs.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

Good link. I added it to the toolkit.

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

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

×
×
  • Create New...