Trimming a Variable

If I had a variable with 4 digits then hyphen and 4 digits how could i get it to trim from that point?

e.g. if anything is 1234-1234//11
I would want to only have this result

1234-1234

But the rule is specific to their being 4 digits then a hyphen then another 4 digits. Can anybody offer guidance?

Thank you.

Try this:

It's the third action with the regular expression that's relevant.

Here's the explanation of the regex:

3 Likes

I'm new 'round here, but thought I would try to help, I did not know about the 'substring' option until someone on this forum informed me.
Maybe this could be of some help.
The 'substring' has a lot of options as to; to/from/last/delete - experiment with the pop-up.
cheers.

2 Likes

Thank you both for your assistance, if i had 12341234 but wanted to add the hyphen to it after the 4th digit how could I add it to get 1234-1234?

Many thank again for your help.

z Substring(s) of variable.kmmacros (3.5 KB)

1 Like

Very helpful thank you

How can I delete the first 10 characters and the final character of a KM variable using AppleScript?

Very easy.

(*
  How can I delete the first 10 characters and the final character of a KM variable using Apple Script?
*)
set someVarStr to "0123456789Text Starting with char 11 ends with deleteX"
set cleanVarStr to text 11 thru -2 of someVarStr
return cleanVarStr
-->Text Starting with char 11 ends with delete

1 Like

Thank you guys... Great help

Hey Folks,

@JMichaelTX has certainly provided the simplest method of doing this with AppleScript, but here's an alternative using a regular expression.

-Chris


AppleScript -- RegEx Find-Replace.kmmacros (3.3 KB)

OK, let's play "How may ways can we do this?" :wink:

Here's two more, both using simple KM Actions:

Example Output

image

MACRO:   Extract Sub-String from String [Example]


#### DOWNLOAD:
<a class="attachment" href="/uploads/default/original/3X/5/0/501625377f20d5fa341b7aaa5363f6d03f1fd518.kmmacros">Extract Sub-String from String [Example].kmmacros</a> (5.2 KB)
**Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.**

---



![image|536x938](upload://bDMOrUht4sJ47sfnYj84RSBGYHc.png)