How to read and copy text between certain symbols?

Hi,

Briefly:
I need to read URL from Chrome, read user id that is within the URL between = and & symbols and use this id to generate another URL.

Details:

Chrome is being opened at URL that looks like this:

http://www.website.com/send.aspx?b1&userid=12345&sendto=

I need to read ‘userid’ value, in this case it would be 12345, could be longer and there is always ‘&’ symbol at the end, ‘=’ at the beginning of id.

Then, using id=12345 I want to enter this URL:

http://www.website.com/viewall.aspx?id=12345

where 12345 is taken from previous step.

Should I use regex to read the line? How this can be done?

Thank you

Hey There,

Use the Chrome action to get the current URL into a variable.

Then use a regular expression to find your string.

Here's how to manage the regex:

RegEx → Find Chrome User-ID String.kmmacros (3.1 KB)

You should be able to figure out the rest, but don't bang your head against the wall too long if you have trouble.

-Chris

Chris, thanks for sharing another great RegEx.

If I understand the RegEx ^.+userid=(\d+) correctly (which is a giant leap of faith), it is looking only for and number of digits after the “userid=” string, right?

I’m wonder if we (the ‘we’ being you), can make this more general by looking any characters between a “?” or “&” + “userid=” and “&” ?

I’m going to try to figure this out myself, but I’m sure you know it off the top of your head. :sunglasses:

Just having some fun . . .

Looks like this works – will for all alphanumeric chars and underscore:
^.+[?&]userid=(\w+)&

EDIT:
But this is better. It also allows for a dash ("–") character as well:
^.+[?&]userid=([\w\-]+)&

Will return X12345ABC

from this:
http://www.website.com/send.aspx?b1?userid=X12345ABC&sendto=

To continue our lesson on RegEx terminology, this RegEx is returning the desired data in a Capture Group, which works well with the KM Search Variable using RegEx Action.

I know, I did it the “hard way”. LOL
OK, now show us the easy way. :wink:

Verified with KM Macro:

Hey JM,

You can probably get away with just this:

=([^&]+)&

Anchoring to the front of the line may be safer in some instances:

^[^=]+=([^&]+)&

Class-negation will cross vertical whitespace when you have multi-line text, so you have to account for that under some circumstances.

Here’s how you might go about this with vanilla AppleScript:

set theURL to "http://www.website.com/send.aspx?b1&userid=12345&sendto=..."
set AppleScript's text item delimiters to "="
set targetText to text item 2 of theURL
set AppleScript's text item delimiters to "&"
set targetText to text item 1 of targetText

If I were to use AppleScript and the Satimage.osax I’d probably do something like this:

set theURL to "http://www.website.com/send.aspx?b1&userid=12345xyz&sendto=..."
set targetText to change "^[^=]+=([^&]+)&.+" into "\\1" in theURL with regexp without case sensitive

The Satimage.osax’s change text is a little faster than its find text command.

On the other hand were still talking ~ ± 0.001 second, so find text might be an option:

# Using Positive Lookbehind Assertion and Positive Lookahead Assertion
set theURL to "http://www.website.com/send.aspx?b1&userid=12345xyz&sendto=..."
set targetText to find text "(?<==)[^&]+(?=&)" in theURL with regexp and string result

As Perl aficionados say: “There’s More Than One Way To Do It (TMTOWTDI or TIMTOWTDI, pronounced Tim Toady)”

-Chris

1 Like