Using Regex to split string to variables

I'm looking to split a string into 5 parts, and assigning each part to separate variables. I am assuming the best option is to use Regex (and I'm complete beginner with regards to Regex).

The string is as follows:

//*[@id='app']/div[2]/div[2]/div[3]/div/div[10]/div[2]/div/div[4]/div[2]/div/div[8]/div/div[2]/div[3]/div/div

I'm looking to split it in the following manner:

First part: Everything up to the third-to-last number.
Second part: The third-to-last number.
Third part: Text between the third-to-last number and the last number.
Fourth part: The last number.
Fifth part: Everything after the last number.

From what I can work out the following Regex formula would split is accordingly:

(.*)(\d)(.*)(\d)(.*)(\d)(.*)

The result being:
First part: //*[@id='app']/div[2]/div[2]/div[3]/div/div[10]/div[2]/div/div[4]/div[2]/div/div[8]/div/div
Second part: 8
Third part: /div[3]/div
Fourth part: 3
Fifth part: /div/div

Having never used Regex before in KM I'm now kid of lost on how I use the formula to assign those parts to the five variables.

Any help greatly appreciated!

The above answer is both correct and meaningless, because it's not related to KM except tangentially. I'm pretty sure it's spam, and have flagged it as such.

Going back to your issue, I have a question at the very top level. You say you want the first part to be everything up to the third-to-last number, and then your example says that number is 2. But isn't the third-to-last number 8, not 2? 3 is the last number, then 2 is second-to-last, and 8 is third-to-last. Which one is it you need to split at?

-rob.

Hi Rob, thank you for pointing out the error (I've edited accordingly).

I need to split at the third last number (8) and the last number (3)

Perhaps prudent to avoid responding or referring to these LLM-bot attempts to set up plausible-looking user accounts ?

( just auto-generated pabulum)

I didn't respond to it, I pointed out it was meaningless. And flagged it as spam for Peter.

Edit: I just zapped its content.

-rob.

1 Like

It also looks like you want to lose the brackets surrounding the numbers? Perhaps it's time to take a big step backwards, as there are potentially simpler solutions at hand.

What is it you're trying to accomplish? What part(s) of that string do you need to keep and work with as variables? Each and every separate part, or only certain parts? The more detail you can provide about what you're trying to do and what you actually need from the string, the better solution you'll get here.

thanks;
-rob.

It's still not entirely clear what you are trying to do. Perhaps a screen shot, annotated in Preview, to show the various capture groups?

Thank you everyone for taking a look at this. I have now worked it out, thanks to ChatGP no less!

Solution below. What I wasn't sure of was how to get the Regex into variable, but now I understand the way to do that is via the Search Variable using Regular Expression action.

The reason for requiring this is that I need to click on 16 different images in a web form.

That's not particularity difficult to do, but the reason for wanting to do it this way was because the starter Xpath regularly changes.

However the two relevant numbers in the xpath, the third last and the last, are always in the same position. Those numbers refer to row and column of the images. By changing those two numbers I can control which image is clicked on.

So now when the xpath changes, rather than have to change 16 different variables manually I can just paste in the new xpath and have all the other variables calculate the new xpaths, as such...

Glad you got it working!

-rob.

The second that I have noticed in the last 24 hours.

Perhaps we should just spam-flag and ignore them, without comment,
so that they can be cleanly excised ?

Yea, and I guess until someone takes away my edit power, I can just remove the content too :).

-rob.

1 Like

User deletion probably sensible too.

I wouldn't be surprised to see a flood of LLM-babble varnishing spam account creation attempts.

Do check this. Will your original third-from-last and last numbers ever be greater than 10, for example?

It might be better to use "matching one or more non-digits" after the first .*:

(.*)(\d+)([^\d]+)(\d+)([^\d]+)(\d+)(.*)

And you don't actually need to capture the numbers you are going to replace:

(.*)(?:\d+)([^\d]+)(\d+)([^\d]+)(?:\d+)(.*)

Speaking of replace -- instead of splitting the string and then recombining the parts you could use a "Search and Replace" action:

image

Putting it all together in a demo:

Change XPath number (s'n'r).kmmacros (4.0 KB)

Image

For giggles, and for those like me who struggle with anything but basic regex, here's a method using pseudo-arrays:

Change XPath number (array).kmmacros (5.2 KB)

Image

...which would be shorter, except that while you can set a pseudo-array element from a "Set Variable to Text" action:
image
...you don't seem to be able to do that directly in the "S'n'R":
image
...which is a shame.

2 Likes

I like. I like a lot! That search and repalce method seems to be much cleaner.