SOLVED: Struggling with \h, \t, etc. to match a tab

Hi -

I'm struggling with a regex task that I thought would be simple: getting a song title out of an email message; I'm using Apple Mail.

My regex works at regex101, shown here:
image

But it does not work in Keyboard Maestro.

I've tried it like this:
searching the mail contents directly

and like this (putting the email contents in a variable first, then searching the variable instead of the message contents directly:
putting the message contents into a variable first

I have also tried \t rather than \h; same result. [edit post to mention this]

I'm wondering if it has something to do with formatted html vs. text, because the mail message uses a table for the info I need – but it does paste into regex101 with tab characters between the columns. That's why I tried putting the text into a variable first, in hopes of getting around that:
screenshot of the formatted message in Mail

I've looked at related discussions like this one and the wiki, which was how I knew to try \h rather than \t.

I'm sure it's user error; just not sure what. Thanks for any help!
CK

Try \t for the tab character—I've never seen \h used, but I know \t works in Keyboard Maestro.

-rob.

Try putting this at the beginning of your regex in your Search action:

(?m)

Nope, \t doesn't work in this case. I tried that first; sorry I didn't mention it.

OK, tried ?m; no change.

Sorry for the obvious question, but have you checked how the %MailContents% token is storing the text? Sometimes this is my issue when my regex works on regex101 but not in Keyboard Maestro. If that isn't the problem, you could try (?<=Track title)[^\S](.*)$ (with or without (?m)). I usually have success with lookbehind in KM, but I'm still learning and rely on some trial and error. [edit] Oh or you could just try [^\S] in place of \h or \t because that also works. :melting_face:

Yes, that's what I was referring to when I mentioned formatted html vs. text because of the table in the email.

I tried (?<=Track title)^\S$ in my macro but it does not work. (It does work at regex101.)

Oh, I see. Could you use a Display in a window action and share the relevant section of the mail contents so we can see what the regex needs to work on?

Solved! I simplified the regex and incorporated the \s
title\s(.*)

image

Why does this work? It is, as @hemicyon and I both suspected, related to the formatting of the email. If I use just the title\s portion of the regex, you can see that instead of a Tab, we find a return:
image

So that's the ticket. Thanks, folks! I just needed to know what to try.

Copying from Mail will get you a bunch of different formats at once -- text, Rich Text, HTML if the email included that... Pasting into your browser involves picking the "best" of those, usually with some extra conversion -- in this case that looks the HTML version with table columns converted to tabs.

The %MailContents% token gets you the plain text contents -- if you look at the message source it'll be the part starting

------=_NextPart_nnn_nnnn_nnnnnnnn.nnnnnnnn
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

(where the ns are a unique hex string). If you look at the source of the message you'll see why it matches how it does in your screen shots.

So not so much the format of the message as the format of that part of the message the token references.

Feels kinda like splitting hairs given that I expected a tab, even saw a tab, but ultimately found a return...? But OK.

That's why you need to view the source of the message to see what's going on here -- it'll all depend on how the plain text version of the HTML message is presented.

<tr><td>cell 1</td><td>cell 2</td></tr>

...might be

cell 1
cell 2

...in plain text, for example. And all that will depend on how the message was generated -- no point me sending myself an HTML message in Mail to demo because that'll almost certainly be different to what they use.

Throw on top of all that the fact that (I think...) the %MailContent% token actually gets the text from the email body via the message's content AppleScript property, and who knows what's happening!

1 Like

FYI - I said try "(?m)" and you said you tried "?m". Not the same thing.

The same goes for [^\S]^\S. We all did the best we could working in the dark on this one.

Yes! I viewed the raw message and found an HTML table, with song title in a container.
The whole message appeared a second time in the raw data using ASCII characters, pipes, etc. to create table structure.

Not long after I figured the regex that would work, the left-side ports on my MacBook died, so after doing a soft reset, SMC reset and diagnostics, I took it to the Apple Store where that board's getting replaced. Hell of an afternoon.

Point being, I don't have the macro at hand, and here on my iPad, I don't see a way to view the raw message data or I'd post it for you.

Thank you!

@hemicyon and @Airy -
I'm sorry; I seem to really have offended both of you.

But... in the dark? In my initial post, I provided a link to the regex101 page, which includes the email text; a screenshot of it (the regex solution); images from two versions of my macro; a screenshot of the original mail so you could see how it was formatted (I didn't know at that time to show the raw message, sorry)...

That seems to have been all wrong. I'm sorry.

No offense taken! We're trying to help, but it's difficult to pattern match without seeing the what the regex is actually matching, as Nige has explained.

But that text is not what was inside the token, which is why the regex wasn't working in Keyboard Maestro but was working on regex101. By the time I asked you to use a display in window action and share what was inside the token, you had already found a solution. When I said we were working in the dark, I meant you too! I don't know how you managed to get the result you were after without seeing what you working with, but you managed it somehow.

Not at all. I am motivated by altruism, ego and the desire to wake myself up in the morning. So if I'm giving you bad vibes, it's probably because I'm groggy.

Oh, geez. I must be spending too much time on Reddit if I’m assuming the crankiest interpretation!

I really appreciate everybody’s help.

As for how I figured it out, I tried each change suggested, and when one didn’t work, I would read up about it at regex101 and see if there was a variation that could possibly work. And I eventually hit the right one.

Thanks again.

1 Like