Sign in to follow this  
Followers 0
crislivinitup

String Regular Expression Help Syntax

12 posts in this topic

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.

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

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

Share this post


Link to post
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.

Share this post


Link to post
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

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

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

Share this post


Link to post
Share on other sites

Slow internet connection, I didn't see your post :).

Yes you're right! Absolutely. A quick question though, is there a way to include special characters in a set "[...]" for example s, H, etc. For my future reference.

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

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

Share this post


Link to post
Share on other sites

Ok, well thanks a lot Beege. I appreciate the help. I'm working on a Front end for making Batch Menus for DosBox... and now I can continue.

Share this post


Link to post
Share on other sites

Happy to help :)

Share this post


Link to post
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)

Share this post


Link to post
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!"

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
Sign in to follow this  
Followers 0

  • Similar Content

    • PhoenixXL
      By PhoenixXL
      RESTRICT EDIT UDF
      Restrict the text that can be entered in an editbox through a String Regular Expression.
      The UDF works be subclassing the control. >Here is a similar UDF based on Windows Message.
       
      Functions
      The UDF is pretty simple and could be modified with ease. User can set their SRE pattern to block the text entered in the editbox Tooltips can be used in case of an error. The title of the Tooltip is Error_2 where the number(2) signifies the pattern that caused the error. The script is made to use function pointer variables rather than direct patterns, to make it more flexible and broader. Supports +ve and -ve SRE pattern matches. See example for better understanding.  
      Note : The Pattern should be made such that every phase of typing is met,
      Example, if the user wants The second letter should be always a digit. Then he should make sure that the pattern is also satisfied when user enters the first character, as through the process of typing.
      Index
      ; #INDEX# ======================================================================================================================= ; Title .........: RestrictEdit ; Version........: 1.0 ; AutoIt Version : 3.3.8.0++ ; Language ......: English ; Author(s)......: Phoenix XL ; Librarie(s)....: WinAPI, Array and GUIEdit ; Description ...: Functions for restricting the text typed in an Edit control through SRE. ; The edit control is subclassed and the original processing is blocked when pattern isn't satisfied. ; =============================================================================================================================== ; #CURRENT# ===================================================================================================================== ;_Restrict_Edit ; =============================================================================================================================== ; #INTERNAL_USE_ONLY# =========================================================================================================== ; HideToolTip_Edit ;_NewWnd_Proc_ ;_SubClass_ ; =============================================================================================================================== ChangeLog
      v1.0 - First Release
      v2.0 - Replaced the SRE pattern parameter(2nd parameter) with function pointer parameter[A script Breaking change]. Added support for positive and negative pattern matches
      Download
      v2.0 RestrictEdit(UDF).7z
      Previous downloads : 161
    • guinness
      By guinness
      Come up with a different regular expression, maybe improved?
      #include <Array.au3> #include <Inet.au3> Local $aSRE = StringRegExp(_INetGetSource('http://www.telize.com/geoip'), '"\w+":"?([^",]+)', 3) _ArrayDisplay($aSRE)
    • Ned
      By Ned
      Could someone make a pattern for this. Been testing out various patterns for hours.

      String

      <div class="profName"><a href="ShowRatings.jsp?tid=1632499">Abaquita, Edwin</a></div>
      My Pattern

      (?<=\QShowRatings.jsp?tid=\E).*(?=\Q</a></div>\E)
      I just need the name (Abaquita, Edwin). The problem with my pattern is the link number (1632499) changes with every name and there are multiple names on a page, also the number of digits varies with each name.

      Thank you in advance.