The Right Way to Change The System Clipboard With RegEx

Hi,

I try to use regex to update the system clipboard. It doesn't work.

What is the right way to do it? After the macro, I just want to use CMD V for having the clipboard updated whith regex.

Here is my macro:

Thanks!
Grégoire

You would need to give us a couple of examples of:

  • typical input strings to copy to the clipboard, and
  • expected output of pasting.

Thanks. Of course. I try to explain.

Usually, I use a text editor to search with this regex:

^(-.+Semaine.+\d{1,2}:.+)(\d)( jour(s?).+)$

And replace with:

$2

And this original text (A):

- Semaine 44: 5 jours sur place.
- Semaine 45: 2 jours en remote (lundi et mardi). Je suis pris ensuite jusqu'à la fin de la semaine 46.
- Semaine 46: 2 jours en remote (jeudi et vendredi).
- Semaine 47: 5 jours sur place.
- Semaine 48: 4 jours en remote (tous les jours sauf mercredi).
- Semaine 49: 4 jours sur place (tous les jours sauf vendredi).
- Semaine 50: 5 jours sur en remote.
- Semaine 1: 5 jours sur place.
- Semaine 3: 5 jours en remote.
- Semaine  4: 5 jours en remote.
- Semaine 5: 5 jours sur place.
- Semaine 6: 2 jours en remote
- Semaine 7: 2 jours en remote
- Semaine 8: 2 jours en remote
- Semaine 10: 2 jour en remote
- Semaine 11: 2 jours en remote
- Semaine 12: 2 jours en remote
- Semaine 13: 1 jour sur place

Becomes the text (B):

5
2
2
5
4
4
5
5
5
5
5
2
2
2
2
2
2
1

I just think that I can do it with KM.

  1. Copy the texte A
  2. Launch the macro
  3. Past the new text B
1 Like

Others will show you something more elegant.

In the meanwhile a step by step JavaScript approach with only a rather basic regex for splitting.

(This draft aims to let you select the text, and get the macro to do the copying and rewriting to clipboard)

day list.kmmacros (6.2 KB)

1 Like

A solution with the native KM action:

You need to do a "multiple-line" search by adding (?m) to the beginning of the search expression.

image

If your source text always conforms to this pattern, you may simplify the search string by getting the number after : (colon and space) every line.

image

2 Likes

Hi @ComplexPoint
Thanks a lot but I prefer to use the KM way :grinning:

Hi @martin

That's it. Thanks you. I forget to add this set up. It works fine.
Thanks for your search string. That’s better :slight_smile: I just add a setup for the digits. Sometimes, I can have 10. It becomes:

(?m)^.+?: (\d{1,2}).+?$

Here is my final Macro.
Manipulate System Clipboard with Regex.kmmacros (3.5 KB)

I don't need to use variable. I just need to manipulate the System Clipboard text.