Musashi Posted May 20, 2020 Share Posted May 20, 2020 I have a problem, which, at first glance, seems quite trivial. But maybe today is one of those days, when you have a plank in front of your head . #include <Array.au3> Local $aLat, $aLon, $aLatLon Local $XMLString = _ "<lat>10.0</lat>" & @CRLF & _ "<lon>1.10</lon>" & @CRLF & _ "<lat>20.0</lat>" & @CRLF & _ "<lon>2.20</lon>" & @CRLF & _ "<lat></lat>" & @CRLF & _ "<lon></lon>" & @CRLF & _ "<lat>40.0</lat>" & @CRLF & _ "<lon>4.40</lon>" $aLat = StringRegExp($XMLString, '<lat>(.+)<\/lat>', 3) $aLon = StringRegExp($XMLString, '<lon>(.+)<\/lon>', 3) $aLatLon = StringRegExp($XMLString, '<lat>(.+)<\/lat>|<lon>(.+)<\/lon>', 3) _ArrayDisplay($aLat, '$aLat') _ArrayDisplay($aLon, '$aLon') _ArrayDisplay($aLatLon, '$aLatLon') The first two arrays ($aLat and $aLon) show the expected result. The third regex ($aLatLon) with | returns 'empty matches' (more precisely Chr(0) ) : A check on https://regex101.com/r/CXR2oL/1 indicates no problems. By the way : The backshlashes <\/ are probably unnecessary, but are required by RegEx101. Certainly someone is smarter than I am today (shouldn't be too hard ). Thanks in advance. "In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move." Link to comment Share on other sites More sharing options...
TheXman Posted May 20, 2020 Share Posted May 20, 2020 (edited) Here's one of many ways that it could be done: #include <Array.au3> Local $aLat, $aLon, $aLatLon Local $XMLString = _ "<lat>10.0</lat>" & @CRLF & _ "<lon>1.10</lon>" & @CRLF & _ "<lat>20.0</lat>" & @CRLF & _ "<lon>2.20</lon>" & @CRLF & _ "<lat></lat>" & @CRLF & _ "<lon></lon>" & @CRLF & _ "<lat>40.0</lat>" & @CRLF & _ "<lon>4.40</lon>" $aLat = StringRegExp($XMLString, '<lat>([^<]*)' , 3) $aLon = StringRegExp($XMLString, '<lon>([^<]*)' , 3) $aLatLon = StringRegExp($XMLString, '<(?:lat|lon)>([^<]*)', 3) _ArrayDisplay($aLat, '$aLat') _ArrayDisplay($aLon, '$aLon') _ArrayDisplay($aLatLon, '$aLatLon') Edited May 20, 2020 by TheXman Removed unnecessary matching criteria from my first example Musashi 1 CryptoNG UDF: Cryptography API: Next Gen jq UDF: Powerful and Flexible JSON Processor | jqPlayground: An Interactive JSON Processor Xml2Json UDF: Transform XML to JSON | HttpApi UDF: HTTP Server API | Roku Remote: Example Script About Me How To Ask Good Questions On Technical And Scientific Forums (Detailed) | How to Ask Good Technical Questions (Brief) "Any fool can know. The point is to understand." -Albert Einstein "If you think you're a big fish, it's probably because you only swim in small ponds." ~TheXman Link to comment Share on other sites More sharing options...
Musashi Posted May 20, 2020 Author Share Posted May 20, 2020 21 minutes ago, TheXman said: Here's one of many ways that it could be done: Yes, thanks for your effort, this is without doubt a feasible way (of many, as usual with RegEx) ! It is also possible to write the following : $aLatLon = StringRegExp($XMLString, '<(?:lat|lon)>([^<]+)</(?:lat|lon)>', 3) BTW : You use * (0 or more) which also matches empty values like <lat></lat>. Small adjustment : + (1 or more) would be more suitable in this particular case. I'm still a bit curious why my in the first posting mentioned variant doesn't work, but that's another story. "In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move." Link to comment Share on other sites More sharing options...
TheXman Posted May 20, 2020 Share Posted May 20, 2020 2 minutes ago, Musashi said: BTW : You use * (0 or more) which also matches empty values like <lat></lat>. Small adjustment : + (1 or more) would be more suitable in this particular case. Yes, I only used an * because I didn't know exactly what you wanted in your result set, all lat/lon or just ones with values. Musashi 1 CryptoNG UDF: Cryptography API: Next Gen jq UDF: Powerful and Flexible JSON Processor | jqPlayground: An Interactive JSON Processor Xml2Json UDF: Transform XML to JSON | HttpApi UDF: HTTP Server API | Roku Remote: Example Script About Me How To Ask Good Questions On Technical And Scientific Forums (Detailed) | How to Ask Good Technical Questions (Brief) "Any fool can know. The point is to understand." -Albert Einstein "If you think you're a big fish, it's probably because you only swim in small ponds." ~TheXman Link to comment Share on other sites More sharing options...
UEZ Posted May 20, 2020 Share Posted May 20, 2020 What about $aLatLon = StringRegExp($XMLString, '>(\d.*)<', 3) ? Musashi 1 Please don't send me any personal message and ask for support! I will not reply! Selection of finest graphical examples at Codepen.io The own fart smells best! ✌Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!¯\_(ツ)_/¯ ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ Link to comment Share on other sites More sharing options...
TheXman Posted May 20, 2020 Share Posted May 20, 2020 (edited) 44 minutes ago, Musashi said: I'm still a bit curious why my in the first posting mentioned variant doesn't work, but that's another story. I know why but I'm not sure I can explain it in a way that others can understand. You had 2 capture groups, let's call them G1 & G2. In general, when the regex engine finds a match, it will stop processing the rest of that match. So when it found a "lat" it put that value in the array and stopped processing. When it was on a "lon" line, It looked for G1 but didn't find it, so it put a blank in the array and then it went on to look for that alternate match ("lon") on that line and put it in the array. So as you can see below, every "lon" line had an empty G1 entry. I hope I was able to explain it in a way that was understandable. Regex: <lat>(.+)<\/lat>|<lon>(.+)<\/lon> G1 G2 <lat>10.0</lat> 10.0 stopped <lon>1.10</lon> blank 1.10 <lat>20.0</lat> 20.0 stopped <lon>2.20</lon> blank 2.20 <lat>40.0</lat> 40.0 stopped <lon>4.40</lon> blank 4.40 Edited May 20, 2020 by TheXman Musashi and Bitnugger 2 CryptoNG UDF: Cryptography API: Next Gen jq UDF: Powerful and Flexible JSON Processor | jqPlayground: An Interactive JSON Processor Xml2Json UDF: Transform XML to JSON | HttpApi UDF: HTTP Server API | Roku Remote: Example Script About Me How To Ask Good Questions On Technical And Scientific Forums (Detailed) | How to Ask Good Technical Questions (Brief) "Any fool can know. The point is to understand." -Albert Einstein "If you think you're a big fish, it's probably because you only swim in small ponds." ~TheXman Link to comment Share on other sites More sharing options...
Musashi Posted May 20, 2020 Author Share Posted May 20, 2020 30 minutes ago, UEZ said: What about Thanks, this variant obviously works too. 11 minutes ago, TheXman said: I hope I was able to explain in a way that was understandable. Yeah, it' s understandable to me. Now that I read it, it's relatively obvious . TheXman 1 "In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move." Link to comment Share on other sites More sharing options...
Bitnugger Posted May 21, 2020 Share Posted May 21, 2020 1 hour ago, TheXman said: Regex: <lat>(.+)<\/lat>|<lon>(.+)<\/lon> G1 G2 <lat>10.0</lat> 10.0 stopped <lon>1.10</lon> blank 1.10 <lat>20.0</lat> 20.0 stopped <lon>2.20</lon> blank 2.20 <lat>40.0</lat> 40.0 stopped <lon>4.40</lon> blank 4.40 Oh yes, that is because two groups are used, was my first thought, but this behavior can not be prevented. Now I'm a little disappointed by regex101.com ... because everything looked perfect there. Well, because of this problem and your good explanation, especially thanks to your great table, I am now a big step ahead. Big thanks! TheXman 1 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now