Community discussions

MikroTik App
 
CuriousKiwi
just joined
Topic Author
Posts: 24
Joined: Thu Feb 05, 2015 2:44 am

Basic XML/string parser function

Fri Apr 24, 2015 12:46 am

Here is a script for a very basic parser, it returns everything in a string found between two strings.

If you need to parse some simple XML or name:value pairs, this should be helpful

It has three parameters:
inputString: The string to search (haystack)
betweenStart,betweenEnd: Return everything from inputString bwteen the end of betweenStart and the start of betweenEnd
If betweenStart is not found in inputString, it will return from the start of inputString
If betweenEnd is not found in inputString, it will return until the end of inputString
:local getBetween do={
	# This is a basic parser, can be used for XML
	# It takes three parameters:
	# inputString - The main string
	# betweenStart - Text AFTER this point will be returned
	# betweenEnd - Text BEFORE this point will be returned
	:local posStart 0;
	:if ([:len $betweenStart] > 0) do={
	:set posStart [:find $inputString $betweenStart]
		:if ([:len $posStart] = 0) do={
			:set posStart 0
		} else={
			:set posStart ($posStart + [:len $betweenStart])
		}
	}

	:local posEnd 9999;
	:if ([:len $betweenEnd] > 0) do={
	:set posEnd [:find $inputString $betweenEnd];
	:if ([:len $posEnd] = 0) do={ :set posEnd 9999 }
	}

	:local result [:pick $inputString $posStart $posEnd];
	:return $result;
}
Examples:
: put [$getBetween inputString="<element>value</element>" betweenStart="<element>" betweenEnd="</element>"]);
Result: value
: put [$getBetween inputString="<element>value</element>" betweenStart="something" betweenEnd="</element>"]);
Result: <element>value
: put [$getBetween inputString="<element>value</element>" betweenStart="something" betweenEnd="something"]);
Result: <element>value</element>

I'll be putting more useful scripts on github: https://github.com/StereotypicalSquirrel
 
jeroenp
Member Candidate
Member Candidate
Posts: 159
Joined: Mon Mar 17, 2014 11:30 am
Location: Amsterdam
Contact:

Re: Basic XML/string parser function

Thu May 05, 2016 12:08 pm

Your function is a great example of why I like named parameters over positional parameters.

Below is a full set of examples showing the permutations of Start/End parameter values when calling it.

Permutations of "something" as parameter:
:put [$getBetween inputString="<element>value</element>" betweenStart="<element>" betweenEnd="</element>"]
value
:put [$getBetween inputString="<element>value</element>" betweenStart="something" betweenEnd="</element>"]
<element>value
:put [$getBetween inputString="<element>value</element>" betweenStart="something" betweenEnd="something"]
<element>value</element>
Permutations of "" as parameter:
:put [$getBetween inputString="<element>value</element>" betweenStart="<element>" betweenEnd=""]
value</element>
:put [$getBetween inputString="<element>value</element>" betweenStart="" betweenEnd="</element>"]
<element>value
:put [$getBetween inputString="<element>value</element>" betweenStart="" betweenEnd=""]
<element>value</element>
Permutations of leaving out the Start/End parameters:
:put [$getBetween inputString="<element>value</element>" betweenStart="<element>"]
value</element>
:put [$getBetween inputString="<element>value</element>" betweenEnd="</element>"]
<element>value
:put [$getBetween inputString="<element>value</element>"]
<element>value</element>
--jeroen

Who is online

Users browsing this forum: eggbean and 18 guests