Multi lines RegExMatch Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
tyyi
Posts: 84
Joined: 23 Dec 2015, 08:51

Multi lines RegExMatch

20 Oct 2018, 10:32

Dear, All.

I have a text file as below;

--------------------------------------------
"index": "0",
"rank": "1",
"id": "54274675",
"name": "mynameis",
"number": "000-0000-1111",
"category": [
"cate1, cate2",
"cate3"
],
"address": "here_address",
"display": "company_name",
"Panorama_1": {
"id": "39823984928",
},
"Panorama_2": {
"id": "92839892389",
},
"broadcastInfo": {
"name": null,
"menu": null
},
---------------------------------------
I show just one starting with "index": "0",
but this text has "index": "500".

Actually, I got all IDs and Names, but...

1) I hope to get just two IDs, the first & third IDs from three, except the second ID, which is in "Panorama_1": {}.
2) I hope to get just the first name, not the second name, which is in "broadcastInfo":{ }.
3) I hope to get cate1, cate2 and cate3 that are in "category": [],

Thank you for all.
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Multi lines RegExMatch

20 Oct 2018, 11:50

is that supposed to be a snippet from a json string(i cant tell for sure cause of the trailing commas and its general handwrittenish feel)?
if so, it would make more sense to parse it with the json lib https://github.com/cocobelgica/AutoHotkey-JSON, then access the properties ure interested in directly
that isnt to say it cant be done with pure regex, but it strikes me as odd doing so
tyyi
Posts: 84
Joined: 23 Dec 2015, 08:51

Re: Multi lines RegExMatch

20 Oct 2018, 22:08

Thank you, swagfag for your answer.
I didn't try to apply it yet.
I've just read about it how to use, so not sure yet if it would work or not. But it seems I wanted.

I will rewrite on my result, after using it.
I really appreciate your response again.
swagfag wrote:
20 Oct 2018, 11:50
is that supposed to be a snippet from a json string(i cant tell for sure cause of the trailing commas and its general handwrittenish feel)?
if so, it would make more sense to parse it with the json lib https://github.com/cocobelgica/AutoHotkey-JSON, then access the properties ure interested in directly
that isnt to say it cant be done with pure regex, but it strikes me as odd doing so
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Multi lines RegExMatch

21 Oct 2018, 07:38

if it is a json string:

Code: Select all

#Warn ClassOverwrite
#Include JSON.ahk

json_str = 
(
	{
		"index": "0",
		"rank": "1",
		"id": "54274675",
		"name": "mynameis",
		"number": "000-0000-1111",
		"category": [
			"cate1, cate2",
			"cate3"
		],
		"address": "here_address",
		"display": "company_name",
		"Panorama_1": {
			"id": "39823984928"
		},
		"Panorama_2": {
			"id": "92839892389"
		},
		"broadcastInfo": {
			"name": null,
			"menu": null
		}
	}
)

Obj := JSON.Load(json_str)
MsgBox % Obj.id
MsgBox % Obj.Panorama_2.id
MsgBox % Obj.broadcastInfo.name
MsgBox % Obj.category[1]
MsgBox % Obj.category[2]
tyyi
Posts: 84
Joined: 23 Dec 2015, 08:51

Re: Multi lines RegExMatch

22 Oct 2018, 00:16

Thank you, swagfag for your reply.

When I applied your script with JSON.ahk, I got some parts of data that I wanted, but didn't get some parts.
I've attached the sample_json_file.txt in this post.

I can get the value of 'sort', 'page' and 'address'. ( the value of this address is 'my_address' )
But I can't do for 'index', 'name' and 'address' in list. ( the value of this address is 'user_address' )

Code: Select all

FileRead, sm_data, sample_json_file.txt

parsed := JSON.Load(sm_data)

MsgBox, % parsed.result.site.sort ; it works.
MsgBox, % parsed.result.site.page ; it works.
MsgBox, % parsed.result.address ; it works. (the value is 'my_address')
MsgBox, % parsed.result.site.list.index ; nothing
MsgBox, % parsed.result.site.list.name ; nothing
MsgBox, % parsed.result.site.list.address ; nothing (the value is 'user_address')
MsgBox, % parsed.result.site.list.streetPanorama.id ; nothing
I'm not sure but I just guessed it is related with the depth of JSON.

Actually, the file I've uploaded has just one index, but the real file I will use has over 500 index.
If I could get the data I didn't get in my script, should I use 'for' loop to get all value for 500 index?

Thank you again. Really appreciated your efforts.

swagfag wrote:
21 Oct 2018, 07:38
if it is a json string:

Code: Select all

#Warn ClassOverwrite
#Include JSON.ahk

json_str = 
(
	{
		"index": "0",
		"rank": "1",
		"id": "54274675",
		"name": "mynameis",
		"number": "000-0000-1111",
		"category": [
			"cate1, cate2",
			"cate3"
		],
		"address": "here_address",
		"display": "company_name",
		"Panorama_1": {
			"id": "39823984928"
		},
		"Panorama_2": {
			"id": "92839892389"
		},
		"broadcastInfo": {
			"name": null,
			"menu": null
		}
	}
)

Obj := JSON.Load(json_str)
MsgBox % Obj.id
MsgBox % Obj.Panorama_2.id
MsgBox % Obj.broadcastInfo.name
MsgBox % Obj.category[1]
MsgBox % Obj.category[2]
Attachments
sample_json_file.txt
(1.9 KiB) Downloaded 25 times
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Multi lines RegExMatch  Topic is solved

22 Oct 2018, 00:37

List is an array containing an object. You access it as follows: parsed.result.site.list[1].address
tyyi
Posts: 84
Joined: 23 Dec 2015, 08:51

Re: Multi lines RegExMatch

22 Oct 2018, 00:57

Thank you, swagfag. :thumbup:
Finally, I get the result that I really wanted.
Your answer solved all of my problems related with JSON.

The script would be more simple than it using RegExMatch.
Without your help, I had to do it with RegExMatch, also I didn't get wanted result.
Thank you again!!!

swagfag wrote:
22 Oct 2018, 00:37
List is an array containing an object. You access it as follows: parsed.result.site.list[1].address

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Anput, Google [Bot], RandomBoy, RussF and 413 guests