SubStringX Searches a string and extract one or more characters, according to unlimited Include-Exclude-Expand criteria.
Finally it returns All the text between the 1st and the last Include Needles.
Best Regards
art
***** UPDATES *****
v1.0 - 10-AUG-2018
- Initial version
- Added parameter "LO" (Left Offset) Number. Number of Characters to omit from the left extreme of Str while searching for 1st Inc Needle.
The latest version is always here
Function and Example:
Code: Select all
v=
(
01. Animals;Cats and Dogs;XX;Home;SA;;;Y;;N;C;SA;
02. Animals;Cats and Dogs;XX;Home;SB;;;Y;;N;C;SB;
03. Animals;Cats and Dogs;XX;Home;SC;;;Y;;N;C;SC;
04. Ugly;Animals;Cats and Dogs;XX;Home;SD;;;Y;;N;C;SD;
05. Lovely;Animals;Cats and Dogs;XX;Home;WORK;;;Y;;Nn;C;SE;Remember to see;
06. wild;Animals;Cats and Dogs;XX;Home;SF;;;Y;;N;C;SF;
07. Animals;Cats and Dogs;XX;Home;SFA;;;Y;;N;C;SFA;
08. Lovely;Animals;Cats and Dogs;XX;Home;WORK;;;N;;Y;C2;SE;Remember to see2;
09. Animals;Cats and Dogs;XX;Home;SFD;;;Y;;N;C;SFD;
10. wild;Animals;Cats and Dogs;XX;Home;SF;;;Y;;N;C;SF;
11. Animals;Cats and Dogs;XX;Home;SFA;;;Y;;N;C;SFA;
12. Lovely;Animals;Cats and Dogs;XX;Home;WORK;;;Y;;N;C3;SE;Remember to see3;
13. Animals;Cats and Dogs;XX;Home;SFD;;;Y;;N;C;SFD;
14. Animals;Cats and Dogs;XX;Home;SK;;;Y;;N;C;SK;
)
Inc:=["Animals;Cats and Dogs;XX;Home;", ";y", ";n;", ";SE;"]
Exc:=["`n"]
LL:=RR:=""
MsgBox,4096,SubStringX - 1, % "[" SubStringX(v, Inc, "", 1, LL, RR) "]`n`n(35-262): LR=" LL "-" RR ; Search with multiple criteria.
/*
[SA;;;Y;;N;C;SA;
02. Animals;Cats and Dogs;XX;Home;SB;;;Y;;N;C;SB;
03. Animals;Cats and Dogs;XX;Home;SC;;;Y;;N;C;SC;
04. Ugly;Animals;Cats and Dogs;XX;Home;SD;;;Y;;N;C;SD;
05. Lovely;Animals;Cats and Dogs;XX;Home;WORK;;;Y;;Nn;C]
*/
LL:=RR:=""
MsgBox,4096,SubStringX - 2, % "[" SubStringX(v, Inc, Exc, 1, LL, RR) "]`n`n(667-681): LR=" LL "-" RR ; All criteria have to exist in any, but the same, line.
; [WORK;;;Y;;N;C3]
LL:=RR:="`n"
MsgBox,4096,SubStringX - 3, % "[" SubStringX(v, Inc, Exc, 1, LL, RR) "]`n`n(626-702): LR=" LL "-" RR ; As previous but returns All the Resultant's line.
; [12. Lovely;Animals;Cats and Dogs;XX;Home;WORK;;;Y;;N;C3;SE;Remember to see3;]
ExitApp
/* PERPOSE: Search a string and extract one or more characters, according to unlimited Include-Exclude-Expand criteria.
Function returns All the text between the 1st and the last Include Needles.
SYNTAX: SubStringX( String, IncludeArray [, ExcludeArray, LeftOffset, LeftExpandString, RightExpandString] )
Str (HayStack) The string whose content is searched. ----- All searches are Case Insensitive -----
Inc (Include Needles) Array of strings. To be a match, All have to exist in the Resultant String and in this order.
Exc (Exclude Needles) Array of strings. To be a match, All have NOT to exist in the Resultant String in any order.
LO (Left Offset) Number. Number of Characters to omit from the left extreme of Str while searching for 1st Inc Needle.
Lex (Expand Left) String. The left extreme of Resultant will be expanded up to this. String itself will be omitted.
Rex (Expand Right) String. The right extreme of Resultant will be expanded up to this. String itself will be omitted.
Regardless of the Resultant's, expansion Lex and Rex vars will return ByRef the L/R position of the Resultant.
*/
;================================================================================ v1.1 | 11-AUG-2018 | by [art]
SubStringX(Str, Inc, Exc="", LO=1, ByRef Lex="", ByRef Rex="") { ; https://autohotkey.com/boards/viewtopic.php?t=53778
ic:=Inc.MaxIndex(), ec:=Exc.MaxIndex(), i1:=StrLen(Inc[1]), i2:=StrLen(Inc[ic]), L1:=LO
Loop { ; ----- All searches are Case Insensitive ----- .
L:=InStr(Str,Inc[1],0,L1), R:=InStr(Str,Inc[ic],0,L1), C:="", po:=1 ; 1st Inc needle pos.
IF !o := SubStr(Str, L, R-L) ; Last Inc needle pos - always after 1st needle.
Break ; Not Found > Break Loop to Exit function.
Loop, % (ic-2) ; Check Inc needles between 1st and last.
IF ((!pi:=InStr(o,Inc[A_Index+1],0,po)) && x:=(1,C:=1,L1:=L+i1)) ; Current Inc needle NotExist (or in wrong...
Continue 2 ; ...order) > NextOffset 1st-last needles.
Else
po:=pi ; po ensures Inc needles Exist in correct order.
Loop, % ec ; Check Exc needles between 1st and last.
IF (InStr(o, Exc[A_Index]) && x:=(1, C:=1, L1:=L+i1)) ; Current Exc needle Exist...
Break ; ... > NextOffset 1st-last needles.
IF (!C && x:=(1,L:=(Lex ?InStr(Str,Lex,0,-StrLen(SubStr(Str,L)))+1 : L),R:=(Rex ?InStr(Str,Rex,0,R): R))) ; (*)
Break ; Found > Break Loop to Exit function.
} Return SubStr(Str, Lex:=(Lex ? L : L+i1), (Rex:=R)-Lex) ; Expand the Resultant (if needed) and assign ByRef values.
} ; (*) Resultant Found (All Inc needles Exist in correct order and All Exc needles NotExist) > Find L/R pos to Expand.