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

beginner needing help:

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




How can I put a variable within a findrx regex pattern:
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:

But I keep against the double quotes problem
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...''

Macro Macro1792
Copy      Help
str lines=

str var_config="config2"
str x

if findrx(lines F"(?s)^\Q[{var_config}]\E(.+?)(?:^\[|\z)" 0 8 x 1)>=0
,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.
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?)
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.
Thanks for your help and info!!
Can you add the example using GetPrivateProfileSection?
Macro Macro1793
Copy      Help
;create ini file for testing
str lines=
str iniFile.expandpath("$temp$\qm48825.ini")

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
,,if(!s[0]) break
,,out s
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.

Forum Jump:

Users browsing this thread: 1 Guest(s)