Regexreplace not working when using string from file

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
hamncheese
Posts: 9
Joined: 08 Feb 2015, 19:07

Regexreplace not working when using string from file

Post by hamncheese » 06 Jul 2022, 22:33

Hello, thank you for anyone reading this, i need some help with a script.
what i need :
- Delete several lines from a .kml file
- insert / write several lines into the .kml file

sample source :

Code: Select all

Sample = 
(
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom">
<Document>
   <Placemark>
	<Style><LineStyle><color>ff0000ff</color></LineStyle><PolyStyle><fill>0</fill></PolyStyle></Style>
	<ExtendedData><SchemaData schemaUrl="#gadm36_IDN_3">
		<SimpleData name="NAME_0">Indonesia</SimpleData>
		<SimpleData name="NAME_1">Jawa Tengah</SimpleData>
		<SimpleData name="NAME_2">Sragen</SimpleData>
		<SimpleData name="NAME_3">Gemolong</SimpleData>
	</SchemaData></ExtendedData>
  </Placemark>
  <Placemark>
	<Style><LineStyle><color>ff0000ff</color></LineStyle><PolyStyle><fill>0</fill></PolyStyle></Style>
	<ExtendedData><SchemaData schemaUrl="#gadm36_IDN_3">
		<SimpleData name="NAME_0">Indonesia</SimpleData>
		<SimpleData name="NAME_1">Jawa Tengah</SimpleData>
		<SimpleData name="NAME_2">Sragen</SimpleData>
		<SimpleData name="NAME_3">Gesi</SimpleData>
	</SchemaData></ExtendedData>
  </Placemark>
  <Placemark>
	<Style><LineStyle><color>ff0000ff</color></LineStyle><PolyStyle><fill>0</fill></PolyStyle></Style>
	<ExtendedData><SchemaData schemaUrl="#gadm36_IDN_3">
		<SimpleData name="NAME_0">Indonesia</SimpleData>
		<SimpleData name="NAME_1">Jawa Tengah</SimpleData>
		<SimpleData name="NAME_2">Sragen</SimpleData>
		<SimpleData name="NAME_3">Gondang</SimpleData>
	</SchemaData></ExtendedData>
  </Placemark>
</Document>
)
Expected Result :

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom">
<Document>
   <Placemark><ExtendedData><SchemaData schemaUrl="#gadm36_IDN_3">
		<SimpleData name="NAME_0">Indonesia</SimpleData>
		<SimpleData name="NAME_1">Jawa Tengah</SimpleData>
		<SimpleData name="NAME_2">Sragen</SimpleData>
		<SimpleData name="NAME_3">Gemolong</SimpleData></SchemaData></ExtendedData><name>Gemolong</name><Style><LineStyle><color>FFFFFFFF</color></LineStyle><PolyStyle><color>FF14B4FF</color><colorMode>normal</colorMode><fill>1</fill></PolyStyle></Style>
  </Placemark>
  <Placemark><ExtendedData><SchemaData schemaUrl="#gadm36_IDN_3">
		<SimpleData name="NAME_0">Indonesia</SimpleData>
		<SimpleData name="NAME_1">Jawa Tengah</SimpleData>
		<SimpleData name="NAME_2">Sragen</SimpleData>
		<SimpleData name="NAME_3">Gesi</SimpleData></SchemaData></ExtendedData><name>Gesi</name><Style><LineStyle><color>FFFFFFFF</color></LineStyle><PolyStyle><color>FF9FE0FF</color><colorMode>normal</colorMode><fill>1</fill></PolyStyle></Style>
  </Placemark>
  <Placemark><ExtendedData><SchemaData schemaUrl="#gadm36_IDN_3">
		<SimpleData name="NAME_0">Indonesia</SimpleData>
		<SimpleData name="NAME_1">Jawa Tengah</SimpleData>
		<SimpleData name="NAME_2">Sragen</SimpleData>
		<SimpleData name="NAME_3">Gondang</SimpleData></SchemaData></ExtendedData><name>Gondang</name><Style><LineStyle><color>FFFFFFFF</color></LineStyle><PolyStyle><color>FF14B4FF</color><colorMode>normal</colorMode><fill>1</fill></PolyStyle></Style>
  </Placemark>
</Document>
</Document>

The Problem :
- i can successfully get the expected result if i use %sample% variable as the input
- the code does nothing when i change the input to fileread


my code so far :

Code: Select all

KmlColoring()
{

sample = 
(
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom">
<Document>
   <Placemark>
	<Style><LineStyle><color>ff0000ff</color></LineStyle><PolyStyle><fill>0</fill></PolyStyle></Style>
	<ExtendedData><SchemaData schemaUrl="#gadm36_IDN_3">
		<SimpleData name="NAME_0">Indonesia</SimpleData>
		<SimpleData name="NAME_1">Jawa Tengah</SimpleData>
		<SimpleData name="NAME_2">Sragen</SimpleData>
		<SimpleData name="NAME_3">Gemolong</SimpleData>
	</SchemaData></ExtendedData>
  </Placemark>
  <Placemark>
	<Style><LineStyle><color>ff0000ff</color></LineStyle><PolyStyle><fill>0</fill></PolyStyle></Style>
	<ExtendedData><SchemaData schemaUrl="#gadm36_IDN_3">
		<SimpleData name="NAME_0">Indonesia</SimpleData>
		<SimpleData name="NAME_1">Jawa Tengah</SimpleData>
		<SimpleData name="NAME_2">Sragen</SimpleData>
		<SimpleData name="NAME_3">Gesi</SimpleData>
	</SchemaData></ExtendedData>
  </Placemark>
  <Placemark>
	<Style><LineStyle><color>ff0000ff</color></LineStyle><PolyStyle><fill>0</fill></PolyStyle></Style>
	<ExtendedData><SchemaData schemaUrl="#gadm36_IDN_3">
		<SimpleData name="NAME_0">Indonesia</SimpleData>
		<SimpleData name="NAME_1">Jawa Tengah</SimpleData>
		<SimpleData name="NAME_2">Sragen</SimpleData>
		<SimpleData name="NAME_3">Gondang</SimpleData>
	</SchemaData></ExtendedData>
  </Placemark>
</Document>
)

	FileSource := "testSource.kml"
	FileResult := "testResult.kml"
	
	FileRead, FileReadVar, %FileSource% 	;This does not work, result shows no change whatsoever from source file
	
	;FileReadVar := sample 			; This works as expected
	
	PolygonColorList =
	(
FF14B4FF	Gemolong
FF9FE0FF	Gesi
FF14B4FF	Gondang
FF1400AA	Jenar
FF9FE0FF	Kalijambe
FF1400FF	Karangmalang
FF14B4FF	Kedawung
	)

FileReadVar := RegexReplace(FileReadVar, "U)<Placemark>.*<ExtendedData>","<Placemark><ExtendedData>")
lines := StrSplit(PolygonColorList, "`n")
columns := []
for index, value in lines
{
	columns.Insert(StrSplit(value, "`t"))	
	Kec := columns[A_Index][2]
	Color := columns[A_Index][1]
	
	Needle01 = 
	(
	Us)(>%Kec%</SimpleData>`n`t</SchemaData></ExtendedData>)
	)

	Needle02 = 
	(
	>%Kec%</SimpleData>`n`t</SchemaData></ExtendedData>
	)
	
	Replace = 
	(
	>%Kec%</SimpleData></SchemaData></ExtendedData><name>%Kec%</name><Style><LineStyle><color>FFFFFFFF</color></LineStyle><PolyStyle><color>%Color%</color><colorMode>normal</colorMode><fill>1</fill></PolyStyle></Style>
	)
	
	
	FoundPos := RegExMatch(FileReadVar, Needle01, m)
	;newStr := StrReplace(FileReadVar, Needle01, m1 . Replace . m3)
	FileReadVar := RegexReplace(FileReadVar, Needle02, Replace)

}

Result := FileReadVar
msgbox % Result
file := FileOpen(FileResult, "w")
file.Write(Result)

}
Return
PS : what im trying to do is :
- set color for a certain polygon in a .kml file
- there are more than 1.000+ polygon than the above example

Full source file :

https://pastebin.com/ZR4NsMm8
Last edited by hamncheese on 06 Jul 2022, 22:58, edited 2 times in total.

User avatar
mikeyww
Posts: 26601
Joined: 09 Sep 2014, 18:38

Re: Regexreplace not working when using string from file

Post by mikeyww » 06 Jul 2022, 22:42

You can use FileRead. Text files often include the carriage returns.

hamncheese
Posts: 9
Joined: 08 Feb 2015, 19:07

Re: Regexreplace not working when using string from file

Post by hamncheese » 06 Jul 2022, 22:51

mikeyww wrote:
06 Jul 2022, 22:42
You can use FileRead. Text files often include the carriage returns.
Hi Mike, thank you for the help, i did try to use FileRead, but somehow regexreplace does nothing, and the result is exactly the same with the source

User avatar
mikeyww
Posts: 26601
Joined: 09 Sep 2014, 18:38

Re: Regexreplace not working when using string from file

Post by mikeyww » 06 Jul 2022, 23:08

Oh, looks like you went backwards instead of forwards in posting a new reply into this thread. Too much for me to read here. I will let others take over here! Best of luck.

User avatar
neovis
Posts: 34
Joined: 12 Jun 2022, 03:56
Location: Republic of Korea
Contact:

Re: Regexreplace not working when using string from file

Post by neovis » 06 Jul 2022, 23:19

Try to change the code as below

Code: Select all

FileRead, FileReadVar, %FileSource%
To this

Code: Select all

FileReadVar := FileOpen(FileSource, "r`r`n").read()
https://www.autohotkey.com/docs/commands/FileOpen.htm#EOL_options

Otherwise, replace all newlines in the variable with linefeed.

Code: Select all

FileReadVar := RegExReplace(FileReadVar, "\R", "`n")

User avatar
MrDodel
Posts: 96
Joined: 28 Apr 2021, 09:03
Location: Event Horizon

Re: Regexreplace not working when using string from file

Post by MrDodel » 07 Jul 2022, 13:11

nvm, my miss understanding.
So much universe, and so little time. GNU Sir Terry.

Post Reply

Return to “Ask for Help (v1)”