Remove some text from clipboard when some characters are unknown

I have text in my clipboard. I want to remove 8 characters starting with xyzq, but I don’t know what comes after those 4 characters. How do I do that?

This "Search and Replace Clipboard" Action should do it:

Each of the periods (".") matches any one character.

xyzq....

This works perfectly well, but I prefer to write it this way:

xyzq.{4}

Because the enumeration tells me at a glance exactly how many characters (or sub-patterns) its looking for.

As regular expressions become more complex they get harder to read, so it’s a good idea to use every little trick to make them more readable.

-Chris

Thanks, Chris.

Readability, like beauty, is in the eye of the beholder.
Your expresstion requires the RegEx understanding of two special RegEx codes:

  • One to match any character
  • One to state the number of characters

While I, and obviously you, might prefer this, the simplest expression is the one I gave.
The reader/user, new to RegEx, needs to understand only of the special RegEx codes. It is pretty easy to count to 4.

Chris is clearly the master of RegEx. But one thing I have learned about RegEx is that there is almost always more than one way of achieving the same results. Chris usually has the most efficient solution.

Oh, fantastic, thanks.

1 Like

I don't know why I didn't think about using this with something other that \d. Good reminder. Thanks!

When I search on @due( I get a match, and I can replace with @due(Beep). This is of course, not my ultimate goal, but good for learning.

When I search for @due(.{9} I don’t get a match, even though there are about 20 characters after “@due(”. Or, I should say, the replacement does not occur, which I think means no match.

Ditto if I search for @due(… No change is made.

I must be doing something wrong, but I don’t know what.

I think this is easy to solve, but first we have to correctly understand your REAL requirements. It is always best, when you first ask a question in these forums, to provide us with real-world examples of both your source data, and the desired results.

I'm make one guess about your requirements, and if it is incorrect, then please provide the real-world examples.

If your source data contains:

some text then @due(Inner Text to Match) and more text

and you want to match "@due(Inner Text to Match)"
and then replace it with "@due(some new text)"

then try this RegEx in the Search:
@due\(.*\)

This will match any number of characters between the parenthesis.

When you are trying to match a literal character, like "(" and ")", that is a RegEx special character, you must escape that character with a backslash "\"

RegEx is a very powerful tool, but has a definite learning curve.
For more info, see Regular Expressions [Keyboard Maestro Wiki]

Thanks, that is very helpful. I am in the very earliest stages of figuring out the map of Regex. I was thinking something was a special character, but I was still working on @, and had not gotten to (! That’s the way life is. I also did not understand how to tell KM that something was a special character anyway.

I admire your patience with new people. It is necessary for the new people to learn, but it also can be hard for experienced users to sustain over time. Thanks for sticking with me.

Like everyone else, I don’t want to read 8 chapters of documentation just want to solve my little bitty problem!

Rol

That may be true, but many of the people here thrive on helping others. It's like a drug.

Preach! :slight_smile:

So, JMichaelTX,

Thanks for your help, that solved THAT problem. Now I need to figure out the date thingy, but don’t help me yet!

Dan Thomas, I hope some day to partake of that drug! I like to give back.

Rol

You're quite welcome.

I totally get that. Neither do I. :smile:

However, with RegEx, some reading/study/testing is really needed to get started. Otherwise, it all appears like some foreign language you never heard of.

###I'd suggest this to get started with RegEx:

  1. Regular Expressions Quick Start
  2. https://regex101.com/
  • A great, free, site to test and build your RegEx
  • I never use any RegEx anywhere without first testing it here

As I mentioned above, Chris (@ccstone) is a real RegEx master. So if you get stuck, post a new topic (with clear requirements) and Chris will often respond.

https://regex101.com/
I never use any RegEx anywhere without first testing it here

Ditto.

Thanks a lot, I am sure this will be helpful. I appreciate the support.

Rol