Simple text extraction ....

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Peter2
Posts: 325
Joined: 21 Sep 2014, 14:38
Location: CH

Simple text extraction ....

17 Feb 2016, 19:57

I'm lost in a simple text extraction between RegExMatch and Match-objects, but I'm sure it can be done with 2 lines. Thanks for help in Advance.

I have simple strings, line by line, created with Fileread

Code: Select all

ProductType="Application"    Name="Inspector" AppVersion="1.0.0" Description="Debugging tools"
ProductType="Application"    Name="Jim Butterfield" AppVersion="1.0.0" Description="this is also a description"
I want to get the values of "name" and "description". I use

Code: Select all

RegExMatch(A_LoopField, "iO)Name=.*?", name_var)

searchvalue := Match.Value(0)
where I hoped to get Name="Inspector" and to trim Name= and ". I get a result for name_var, but can not extract the value.
What's wrong? :?
Peter (AHK Beginner) / Win 10 x64, AHK Version v1.1.33
kon
Posts: 1756
Joined: 29 Sep 2013, 17:11

Re: Simple text extraction ....

17 Feb 2016, 20:18

.*? - Match between zero and unlimited times, as few times as possible. In the example below I required it match the closing quote to signify the end of the match. "iO)Name="".*?""".

Also, it should be name_var.Value(0) not Match.Value(0).

Code: Select all

Test =
(
ProductType="Application"    Name="Inspector" AppVersion="1.0.0" Description="Debugging tools"
ProductType="Application"    Name="Jim Butterfield" AppVersion="1.0.0" Description="this is also a description"
)

Loop, Parse, Test, `n, `r
{
    RegExMatch(A_LoopField, "iO)Name="".*?""", name_var)
    MsgBox, % name_var.Value(0)
}
Or with a named capturing group:

Code: Select all

    RegExMatch(A_LoopField, "iO)Name=""(?P<name>.*?)""", name_var)
    MsgBox, % name_var.name
Side note: since you are already parsing it line-by-line, you could accomplish the same thing by using InStr() and SubStr().
Peter2
Posts: 325
Joined: 21 Sep 2014, 14:38
Location: CH

Re: Simple text extraction ....

18 Feb 2016, 02:50

It is (in large part) clear now - as usual after an explanation here in the forum :bravo:

But I don't understand the searching of quotes:

Code: Select all

search for 'abc'  -->> parameter "abc"
search for 'Name="Inspector"'   -->> parameter "Name=""Inspector"""
It seems that the doubled quotes "" are not an "escaping", and they are not a concatenation of parts like "abc""abc", and they are not literal \Q...\E.
How is it working?
kon wrote:...you could accomplish the same thing by using InStr() and SubStr().
But InStr() gives me only the position, not the length and not the value (and both differs from line to line)
Peter (AHK Beginner) / Win 10 x64, AHK Version v1.1.33
kon
Posts: 1756
Joined: 29 Sep 2013, 17:11

Re: Simple text extraction ....

18 Feb 2016, 11:27

In any AHK expression two quotes within a string yields a literal quote. https://autohotkey.com/docs/commands/_E ... tm#Remarks

This is what I meant about using Instr / SubStr

Code: Select all

    StartPos := InStr(A_LoopField, "Name=""") + 6 ; Position of name + offset
    EndPos := InStr(A_LoopField, """",, StartPos) ; Position of closing quote
    MsgBox, % SubStr(A_LoopField, StartPos, EndPos - StartPos)
Peter2
Posts: 325
Joined: 21 Sep 2014, 14:38
Location: CH

Re: Simple text extraction ....

18 Feb 2016, 11:52

kon wrote:In any AHK expression two quotes within a string yields a literal quote. https://autohotkey.com/docs/commands/_E ... tm#Remarks ...
Thanks. I looked at the help of RegExMatch, where I could find no info about double quotes
kon wrote:...This is what I meant about using Instr / SubStr...
It will take time that I get a comprehensive view over all standard functions .. :roll:

Thanks for help. :beer:
Peter (AHK Beginner) / Win 10 x64, AHK Version v1.1.33

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: filipemb, kingina, mcd and 193 guests