I’ve written an XML parser in Python and have just added functionality to read a further script from a different directory.
I’ve got two args, first is the path where I’m parsing XML. Second is a string in another XML file which I want to match with the first path;
arg1 = \work\parser\main\tools\app\shared\xml\calculators\2012\example\calculator
path = calculators/2012/example/calculator
How can I compare the two strings to match identify that they’re both referencing the same thing and also, how can I strip calculator from either string so I can store that & use it?
edit
Just had a thought. I have used a Regex to get the year out of the path already with year = re.findall(r"\.(\d{4})\.", path) following a problem Python has with numbers when converting the path to an import statement.
I could obviously split the strings and use a regex to match the path as a pattern in arg1 but this seems a long way round. Surely there’s a better method?
Here I am assuming you are actually talking about strings, and not file paths – for which @mgilson’s suggestion is better
Well first you need to identify what you mean by “the same thing”
At first glance it seems that if the the second string ends with the first string with the reversed slash, you have a match.
You also need to decide if you want to strip the first calculator, the last calculator or any occurance of calculator in the string.
If you just want to remove the last string after the separator, then its simply:
Now to get the orignal string back, without the last bit: