JustMe

StringRegExp

8 posts in this topic

#1 ·  Posted

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

; data_file_1.csv
; ===============
; SEARCH;CRITERIA;50;100;J;S1;
; MORE;SEARCH;CRITERIA;25;125;J;S1;
; SOME;MORE;COMPLEX;SEARCH;CRITERIA;50;100;Y;S2;
; EOF

; The numeric variables a cusor column and row positions
; Y is a parameter determining if a MouseClick("left") should be  performed
; S1 and S2 are parameters determining if how long the sleep command should be
 
Local $screen_file_1 = FileOpen("data_file_1.csv", 0)     

Local $screen_line_1 = FileRead($screen_file_1)  

$array_line_1 = StringRegExp($screen_line_1,"(MORE;SEARCH;CRITERIA)(?: )?(.+)",2)       

_ArrayDisplay($array_line_1)


; Search returns three arrays as text file
; [0] string of all of the date in the selected line beginning with the search criteria ie:
; MORE;SEARCH;CRITERIA;25;125;J;S1;

; [1] Search criteria ie.:
; MORE;SEARCH;CRITERIA

; [2] rest Data found in the line after the search criteria ie
; ;25;125;J;S1;

; This ist good but but as the search criteria are variable in length
; the variables for MouseClick("left") and sleep are always in a diferend column.
;
; Now the Problem
; I tried this:
;
; data_file_2.csv
; ===============
; 50;100;J;S1;SEARCH;CRITERIA;
; 25;125;J;S1;MORE;SEARCH;CRITERIA;
; 50;100;J;S2;SOME;MORE;COMPLEX;SEARCH;CRITERIA;
; EOF
;
; ie. the MouseClick("left") and sleep parameters are always in the first and second column
; no matter how long the search criteria is.
; This is what I get

Local $screen_file_2 = FileOpen("data_file_2.csv", 0)     

Local $screen_line_2 = FileRead($screen_file_2)  

$array_line_2 = StringRegExp($screen_line_2,"(MORE;SEARCH;CRITERIA)(?: )?(.+)",2)       

_ArrayDisplay($array_line_2)

The Result is logical:
; [0] = MORE;SEARCH;CRITERIA;
; [1] = MORE;SEARCH;CRITERIA
; [2] = ;

; My Question:
; Is it somehow possible to get the rest of the data in line preceeding the search crieria?

 

data_file_1.csv

data_file_2.csv

Share this post


Link to post
Share on other sites



#2 ·  Posted

Plz edit/improve ya post.

  • Format autoit code as 'code'
    with syntax highlighting it'll be more 'inviting' for ppl to study it
  • choose a more specific topic
  • give some feedback in case you already solved it

Share this post


Link to post
Share on other sites

#3 ·  Posted

On 5/19/2017 at 7:16 AM, JustMe said:

....

; My Question:

; Is it somehow possible to get the rest of the data in line preceding the search criteria?

....

You mention "Y" in your post.  But everything points to "J".  So I included both. "[JY]" in the regular expression pattern.

Hope this is what you want.

#include <Array.au3>

Local $sRegExpPattern = "(\d+;\d+;[JY];S[12])"

;----------------------------------------------------------------------------------
Local $FileName = "data_file_1.csv" ; In same ditectory as this script. Otherwise use pull path file name.
If FileExists($FileName) Then FileDelete($FileName)
FileWrite($FileName, _
        "; SEARCH;CRITERIA;50;100;J;S1;" & @CRLF & _
        "; MORE;SEARCH;CRITERIA;25;125;J;S1;" & @CRLF & _
        "; SOME;MORE;COMPLEX;SEARCH;CRITERIA;50;100;J;S1;" & @CRLF)

$array = StringRegExp(FileRead("data_file_1.csv"), $sRegExpPattern, 3) ; $STR_REGEXPARRAYGLOBALMATCH (3)
_ArrayDisplay($array, "1")
FileDelete($FileName) ; Clean up

;----------------------------------------------------------------------------------
$FileName = "data_file_2.csv"
If FileExists($FileName) Then FileDelete($FileName)
FileWrite($FileName, _
        "; 50;100;J;S1;SEARCH;CRITERIA;" & @CRLF & _
        "; 25;125;J;S1;MORE;SEARCH;CRITERIA;" & @CRLF & _
        "; 50;100;J;S2;SOME;MORE;COMPLEX;SEARCH;CRITERIA;" & @CRLF)

$array = StringRegExp(FileRead("data_file_2.csv"), $sRegExpPattern, 3) ; $STR_REGEXPARRAYGLOBALMATCH (3)
_ArrayDisplay($array, "2")
FileDelete($FileName) ; Clean up

 

Share this post


Link to post
Share on other sites

#4 ·  Posted

Hallo Robinson,

Thanks for your comments.  As you can see I am new .  I will do this better next time.

Thanks Malkey. I see that I did not phrase my question correctly.

Your script returns numeric, YN, and S1 or S2 in all lines of the file. This  is not what I need.
Sorry for the confusion with "Y" and "J". This is for a German application and I got mixed up when transcribing for this enquiry.
 
The first step is to get the line I am looking for in the file.  This for the purposes of this test I have used
"MORE;SEARCH;CRITERIA"
as the search criteria

$array_line_1 = StringRegExp($screen_line_1,"(MORE;SEARCH;CRITERIA)(?: )?(.+)",2)     

In "real life" the file is much longer and the search criteria will vary from search to search.
The search criteria will, however, always be Unique to one line of the the file.

Searching using

$array_line_1 = StringRegExp($screen_line_1,"(MORE;SEARCH;CRITERIA)(?: )?(.+)",2)     

with data_file_1.csv  also get the data after the search criteria. (ie. 25;125;J;S1)
I can work with this but I would like to organise the file as in data_file_2.csv

Searching using

$array_line_1 = StringRegExp($screen_line_1,"(MORE;SEARCH;CRITERIA)(?: )?(.+)",2)     

with data_file_2.csv does not return the fields  ;  25;125;Y;S1
So I am looking for a method to get the variables preceding the search criteria but in the same record found by the search criteria.

I hope that makes my request clearer. Many Thanks for your help.

 

Share this post


Link to post
Share on other sites

#5 ·  Posted

Maybe this ?

#include <Array.au3>

$search = "MORE;SEARCH;CRITERIA"

$res1 = _Get("data_file_1.csv", $search)
_ArrayDisplay($res1, "1")
$res2 = _Get("data_file_2.csv", $search)
_ArrayDisplay($res2, "2")

Func _Get($FileName, $search)
  $a = StringRegExp(FileRead($FileName), "(?m)^.*?\Q" & $search & "\E.*$", 1) 
  If IsArray($a) Then 
    $line = $a[0]
    $num = StringRegExpReplace($line, '.*(\d+;\d+;[JY];S[12]).*', "$1")
    If not @extended Then Return 0
    $anum = StringSplit($num, ";")
    $anum[0] = $search
    Return $anum
  EndIf
  Return 0
EndFunc

 

Share this post


Link to post
Share on other sites

#6 ·  Posted

Magic !

Many Thanks and Issue closed

Share this post


Link to post
Share on other sites

#7 ·  Posted

You can use a site like this to experimentwith regex
http://regexr.com/
On the left it has examples and references for what symbols do what, in the middle yo can paste whatever you want and then work on the regex at the top and see in realtime what is being matched. hovering your mouse over highlighted parts of the text will go into detail of what is being returned.

StringRegExp by default returns true or false, but mode 3 will return an array the same as the matching groups when you hover over them in regexr, making it super simple to know exactly what autoit will do when you use stringregexp with the same expression.


0x616e2069646561206973206c696b652061206d616e20776974686f7574206120626f64792c20746f206669676874206f6e6520697320746f206e657665722077696e2e2e2e2e

Share this post


Link to post
Share on other sites

#8 ·  Posted

Hi corgano. Thanks for the info. I will give it a try.

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