Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
beginner: regex ini-like file and variable within regex
#1
Hello,

beginner needing help:

1)
What is the best way to retrieve everthing beneath "[config2]" until the next [...], I want to retrieve "cde" , "fgh" and "ijk"

[config1]
abc

[config2]
cde
fgh
ijk


[config3]
etc...


2)
How can I put a variable within a findrx regex pattern:
Code:
Copy      Help
findrx(lines "^\[config2](?:\r?\n(?:[^[\r\n].*)?)*" 0 8 x)

I want the string "config2" be replaced by a variable for exmple "var_config"
findrx(lines "^\[var_config](?:\r?\n(?:[^[\r\n].*)?)*" 0 8 x)
I tried {var_config}

I tried:
RunTextAsFunction2

But I keep against the double quotes problem
Code:
Copy      Help
str result="findrx(lines "^\[config2](?:\r?\n(?:[^[\r\n].*)?)*" 0 8 x)"

I also tried to replace the inner pair of double quotes with double single quotes like this: '' ... regex...''

thanks!
#2
Macro Macro1792
Code:
Copy      Help
out
str lines=
;[config1]
;abc
;
;[config2]
;cde
;fgh
;ijk
;
;[config3]
;etc...

str var_config="config2"
str x

if findrx(lines F"(?s)^\Q[{var_config}]\E(.+?)(?:^\[|\z)" 0 8 x 1)>=0
,x.trim
,out x
else out "not found"

Also can be used API function GetPrivateProfileSection.

To add variables to a string, there are 2 ways:
1. Put F before the string, and insert variables enclosed in { }. Example: F"string {variable} string"
2. Use function str.format.
When using in regular expression, enclose variables in \Q \E. Then variable text is interpreted as simple text, not as regular expression.
#3
thanks man!

you have no idea how relieved I am seeing the correct result in the output window!!

- Did you actually build this powerhouse tool yourself? And is Quick Macros programmed/build using the Quick Macro scripting language itself?
- On what language is the Quick Macro scripting language based? (or this language build from the ground up?)
#4
Quick Macros is programmed using C++ (>50%) and QM language (<50%). Most of code is mine, but parts are from various libraries, for example PCRE for regular expressions, scintilla for editor, mailbee for email.

The language is not based on other languages. Something is similar to C++, something to some other languages.
#5
Thanks for your help and info!!
#6
Can you add the example using GetPrivateProfileSection?
#7
Macro Macro1793
Code:
Copy      Help
out
;---------------
;create ini file for testing
str lines=
;[config1]
;abc
;
;[config2]
;cde=1
;fgh=2
;ijk=3
;
;[config3]
;etc...
str iniFile.expandpath("$temp$\qm48825.ini")
lines.setfile(iniFile)
;---------------

str var_config="config2"
str x

int nSize=100*1024; x.all(nSize)
x.fix(GetPrivateProfileSection(var_config x nSize iniFile))
if x.len
,;GetPrivateProfileSection returns multistring; show each string
,lpstr s=x
,rep
,,if(!s[0]) break
,,out s
,,s+len(s)+1
else out "not found"

;this is not perfect code. 1. Need to ensure that nSize is big enough; 2. Need to use Unicode function version to support Unicode in ini file path.
#8
Thanks.


Forum Jump:


Users browsing this thread: 1 Guest(s)