3
votes

I have a file that I get from pulling out values from a Microsoft Lync conversation that has RTF formatting tags. An example file would be like:

{\rtf1\fbidis\ansi\ansicpg1252\deff0\nouicompat\deflang1033{\fonttbl{\f0\fnil\fcharset0 >Segoe UI;}{\f1\fnil Segoe UI;}} {\colortbl ;\red0\green0\blue0;} {*\generator Riched20 15.0.4420}{*\mmathPr\mwrapIndent1440 }\viewkind4\uc1 \pard\cf1\embo\f0\fs20 Craig...\embo0 \embo please\embo0 \embo close\embo0 \embo >out\embo0 \embo of\embo0 \embo your\embo0 \embo old\embo0 \embo client\embo0 \embo >and\embo0 \embo re-open\embo0\f1\par {*\lyncflags rtf=1}}

Using Lua scripting I'm trying to remove the RTF tags and just pull out the text of the conversation. So the result of my function should be:

Craig... please close out of your old client and re-open

I've tried using a string.gsub with a regex to match the patterns and replace them with a blank space to only leave the text but it's not working. Here is the code that I have so far for the string.gsub:

result = string.gsub(s, "\{\*?\\[^{}]+}|[{}]|\\\n?[A-Za-z]+\n?(?:-?\d+)?[ ]?", " ")

Any suggestions would be greatly appreciated!

Additional:

[email protected] @ 2013-01-18 17:48:03Z (TO: [email protected])

{\rtf1\fbidis\ansi\ansicpg1252\deff0\nouicompat\deflang1033{\fonttbl{\f0\fnil\fcharset0 Segoe UI;}{\f1\fnil Segoe UI;}} {\colortbl ;\red0\green0\blue0;} {*\generator Riched20 15.0.4420}{*\mmathPr\mwrapIndent1440 }\viewkind4\uc1 \pard\cf1\embo\f0\fs20 works\embo0 \embo for\embo0 \embo me..\embo0 \embo how\embo0 \embo about\embo0 \embo embedding\embo0 \embo pictures?\embo0\f1\par {*\lyncflags rtf=1}}

[email protected] @ 2013-01-18 17:48:57Z (TO: [email protected])

{\rtf1\fbidis\ansi\ansicpg1252\deff0\nouicompat\deflang1033{\fonttbl{\f0\fnil\fcharset0 Segoe UI;}{\f1\fnil Segoe UI;}} {\colortbl ;\red0\green0\blue0;} {*\generator Riched20 15.0.4420}{*\mmathPr\mwrapIndent1440 }\viewkind4\uc1 \pard\cf1\embo\f0\fs20 I\embo0 \embo see\embo0 \embo it\embo0\f1\par {*\lyncflags rtf=1}}

[email protected] @ 2013-01-18 17:49:27Z (TO: [email protected])

{\rtf1\fbidis\ansi\ansicpg1252\deff0\nouicompat\deflang1033{\fonttbl{\f0\fnil\fcharset0 Segoe UI;}{\f1\fnil Segoe UI;}} {\colortbl ;\red0\green0\blue0;} {*\generator Riched20 15.0.4420}{*\mmathPr\mwrapIndent1440 }\viewkind4\uc1 \pard\cf1\embo\f0\fs20 let's\embo0 \embo try\embo0 \embo a\embo0 \embo meeting.\embo0\f1\par {*\lyncflags rtf=1}}

2
Welcome to StackOverflow! What output does the regex you've given result in? - andrewdotnich
If in the end you are still not satisfied with the pattern matching solution you might want to check you the lpeg library - hugomg

2 Answers

2
votes

Lua patterns don't have or operators (|) or optional groupings ((?:...)?). Something like this may work:

s:match("{(.+)}"):gsub("%b{}", ""):gsub("\\%w+", "")

will return:

"    Craig...  please  close  >out  of  your  old  client  >and  re-open "

First gsub removes all pairs of {} along with their content, the second gsub removes all rtf tags (although there seem to be some that allow spaces in them, so you may need to tweak the pattern).

0
votes

Try this:

local s = '{\rtf1\fbidis\ansi\ansicpg1252\deff0\nouicompat\deflang1033{\fonttbl{\f0\fnil\fcharset0 >Segoe UI;}{\f1\fnil Segoe UI;}} {\colortbl ;\red0\green0\blue0;} {*\generator Riched20 15.0.4420}{*\mmathPr\mwrapIndent1440 }\viewkind4\uc1 \pard\cf1\embo\f0\fs20 Craig...\embo0 \embo please\embo0 \embo close\embo0 \embo >out\embo0 \embo of\embo0 \embo your\embo0 \embo old\embo0 \embo client\embo0 \embo >and\embo0 \embo re-open\embo0\f1\par {*\lyncflags rtf=1}}\n'
    ..'{\rtf1\fbidis\ansi\ansicpg1252\deff0\nouicompat\deflang1033{\fonttbl{\f0\fnil\fcharset0 Segoe UI;}{\f1\fnil Segoe UI;}} {\colortbl ;\red0\green0\blue0;} {*\generator Riched20 15.0.4420}{*\mmathPr\mwrapIndent1440 }\viewkind4\uc1 \pard\cf1\embo\f0\fs20 works\embo0 \embo for\embo0 \embo me..\embo0 \embo how\embo0 \embo about\embo0 \embo embedding\embo0 \embo pictures?\embo0\f1\par {*\lyncflags rtf=1}}\n'
    ..'{\rtf1\fbidis\ansi\ansicpg1252\deff0\nouicompat\deflang1033{\fonttbl{\f0\fnil\fcharset0 Segoe UI;}{\f1\fnil Segoe UI;}} {\colortbl ;\red0\green0\blue0;} {*\generator Riched20 15.0.4420}{*\mmathPr\mwrapIndent1440 }\viewkind4\uc1 \pard\cf1\embo\f0\fs20 I\embo0 \embo see\embo0 \embo it\embo0\f1\par {*\lyncflags rtf=1}}\n'
    ..'{\rtf1\fbidis\ansi\ansicpg1252\deff0\nouicompat\deflang1033{\fonttbl{\f0\fnil\fcharset0 Segoe UI;}{\f1\fnil Segoe UI;}} {\colortbl ;\red0\green0\blue0;} {*\generator Riched20 15.0.4420}{*\mmathPr\mwrapIndent1440 }\viewkind4\uc1 \pard\cf1\embo\f0\fs20 let\'s\embo0 \embo try\embo0 \embo a\embo0 \embo meeting.\embo0\f1\par {*\lyncflags rtf=1}}\n'
local text = string.gsub(s, '{(.-)}[}]?', ''):gsub('embo',''):gsub('0',''):gsub('iewkind4uc1 pardcf1',''):gsub('1par',''):gsub('s2',''):gsub('>','')
print(text)

output

Craig... please close out of your old client and re-open
works for me.. how about embedding pictures?
I see it
let's try a meeting.