;;多行文字样式清除
(defun C:mtx(/ ss i ent elist )
(if (setq ss(ssget '((0 . "TEXT,MTEXT"))))
(progn
(setq i 0)
(repeat (sslength ss)
(setq ent (ssname ss i))
(setq elist (entget ent))
(entmod
(subst
(cons 1
(mtext2text (cdr(assoc 1 elist)))
)
(assoc 1 elist)
elist
)
)
(setq i (1+ i))
)
)
)
)
;提取多行文字,去除无用格式符号--来自明经
(defun mtext2text(MTextString / regex s)
(setq regex(vlax-create-object "Vbscript.RegExp")) ;引用正则表达式控件
(vlax-put-property regex "IgnoreCase" 0) ;不忽略大小写
(vlax-put-property regex "Global" 1) ;匹配方式,全文字匹配
(setq s MTextString)
;替换\\字符
(vlax-put-property regex "Pattern" "\\\\\\\\")
(setq s(vlax-invoke-method regex "Replace" s (chr 1)))
;替换\{字符
(vlax-put-property regex "Pattern" "\\\\{")
(setq s(vlax-invoke-method regex "Replace" s (chr 2)))
;替换\}字符
(vlax-put-property regex "Pattern" "\\\\}")
(setq s(vlax-invoke-method regex "Replace" s (chr 3)))
;删除段落缩进格式
(vlax-put-property regex "Pattern" "\\\\pi(.[^;]*);")
(setq s(vlax-invoke-method regex "Replace" s ""))
;删除制表符格式
(vlax-put-property regex "Pattern" "\\\\pt(.[^;]*);")
(setq s(vlax-invoke-method regex "Replace" s ""))
;删除堆迭格式
(vlax-put-property regex "Pattern" "\\\\S(.[^;]*)(\\^|#|\\\\)(.[^;]*);")
(setq s(vlax-invoke-method regex "Replace" s ""))
;删除字体、颜色、字高、字距、倾斜、字宽、对齐格式
(vlax-put-property regex "Pattern" "(\\\\F|\\\\f|\\\\C|\\\\H|\\\\\T|\\\\Q|\\\\W|\\\\A)(.[^;]*);")
(setq s(vlax-invoke-method regex "Replace" s ""))
;删除下划线、删除线格式
(vlax-put-property regex "Pattern" "(\\\\L|\\\\O|\\\\l|\\\\o)")
(setq s(vlax-invoke-method regex "Replace" s ""))
;删除不间断空格格式
(vlax-put-property regex "Pattern" "\\\\~")
(setq s(vlax-invoke-method regex "Replace" s ""))
;删除换行符格式
(vlax-put-property regex "Pattern" "\\\\P")
(setq s(vlax-invoke-method regex "Replace" s ""))
;删除换行符格式(针对Shift+Enter格式)
(vlax-put-property regex "Pattern" "\n")
(setq s(vlax-invoke-method regex "Replace" s ""))
;删除{}
(vlax-put-property regex "Pattern" "({|})")
(setq s(vlax-invoke-method regex "Replace" s ""))
;替换回\\,\{,\}字符
(vlax-put-property regex "Pattern" "\\x01")
(setq s(vlax-invoke-method regex "Replace" s "\\"))
(vlax-put-property regex "Pattern" "\\x02")
(setq s(vlax-invoke-method regex "Replace" s "{"))
(vlax-put-property regex "Pattern" "\\x03")
(setq s(vlax-invoke-method regex "Replace" s "}"))
(vlax-release-object regex)
s
)
public struct Strep
{
public string Patstr { get; set; }
public string Repstr { get; set; }
}
public static string GetMTextRegstr(string input)
{
string tXTstr = input;
List<Strep> replst = new List<Strep>()
{
new Strep(){Patstr= @"\\\\" ,Repstr=((char)1).ToString()},
new Strep(){Patstr= @"\\{" , Repstr=((char)2).ToString()},
new Strep(){Patstr= @"\\}" , Repstr=((char)3).ToString()},
new Strep(){Patstr= @"\\pi(.[^;]*);" , Repstr=""},
new Strep(){Patstr= @"\\pt(.[^;]*);" , Repstr=""},
new Strep(){Patstr= @"\\S(.[^;]*)(^|#|\\)(.[^;]*);" , Repstr=""},
new Strep(){Patstr= @"(\\F|\\f|\\C|\\H|\\T|\\Q|\\W|\\A)(.[^;]*);" , Repstr=""},
new Strep(){Patstr= @"(\\L|\\O|\\K|\\l|\\o)" , Repstr=""},
new Strep(){Patstr= @"\\~" , Repstr=""},
new Strep(){Patstr= @"\\pql;" , Repstr=""},
new Strep(){Patstr= @"\\pqc;" , Repstr=""},
new Strep(){Patstr= @"\\pqr;" , Repstr=""},
new Strep(){Patstr= @"\\pxqc;" , Repstr=""},
new Strep(){Patstr="\\\\P" ,Repstr=" "},
new Strep(){Patstr="\n" ,Repstr=" "},
new Strep(){Patstr= @"({|})" , Repstr=""},
new Strep(){Patstr= @"\x01" , Repstr=@"\\\\"},
new Strep(){Patstr= @"\x02" , Repstr="{"},
new Strep(){Patstr= @"\x03" , Repstr="}"}
};
foreach (Strep sr in replst)
{
if (Regex.IsMatch(tXTstr, sr.Patstr)) tXTstr = Regex.Replace(tXTstr, sr.Patstr, sr.Repstr);
}
return tXTstr;
}
2020.12.28优化版——-推荐
public struct Strep
{
public string Patstr { get; set; }
public string Repstr { get; set; }
}
public static string GetMTextRegstr(string input)
{
string tXTstr = input;
List<Strep> replst = new List<Strep>()
{
//替换\\字符
new Strep(){Patstr= @"\\\\" ,Repstr=((char)1).ToString()},
//替换\{字符
new Strep(){Patstr= @"\\{" , Repstr=((char)2).ToString()},
//替换\}字符
new Strep(){Patstr= @"\\}" , Repstr=((char)3).ToString()},
//删除段落缩进格式
new Strep(){Patstr= @"\\pi(.[^;]*);" , Repstr=""},
//删除制表符格式
new Strep(){Patstr= @"\\pt(.[^;]*);" , Repstr=""},
//删除字体、颜色、字高、字距、倾斜、字宽、对齐格式
new Strep(){Patstr= @"(\\F|\\f|\\C|\\H|\\T|\\Q|\\W|\\A)(.[^;]*);" , Repstr=""},
//删除下划线、删除线格式
new Strep(){Patstr= @"(\\L|\\O|\\K|\\l|\\o)" , Repstr=""},
//删除不间断空格格式
new Strep(){Patstr= @"\\~" , Repstr=""},
new Strep(){Patstr= @"\\pse1;" , Repstr=""},
new Strep(){Patstr= @"\\pql;" , Repstr=""},
new Strep(){Patstr= @"\\pqc;" , Repstr=""},
new Strep(){Patstr= @"\\pqr;" , Repstr=""},
new Strep(){Patstr= @"\\pxqc;" , Repstr=""},
//删除换行符格式
new Strep(){Patstr="\\\\P" ,Repstr=" "},
//删除换行符格式(针对Shift+Enter格式)
new Strep(){Patstr="\n" ,Repstr=" "},
//删除{}
new Strep(){Patstr= @"({|})" , Repstr=""},
//替换回\\,\{,\}字符
new Strep(){Patstr= @"\x01" , Repstr=@"\\\\"},
new Strep(){Patstr= @"\x02" , Repstr="{"},
new Strep(){Patstr= @"\x03" , Repstr="}"}
};
foreach (Strep sr in replst)
{
if (Regex.IsMatch(tXTstr, sr.Patstr)) tXTstr = Regex.Replace(tXTstr, sr.Patstr, sr.Repstr);
}
return Stacking(tXTstr);
/// <summary>
/// 清除堆叠格式
/// </summary>
/// <param name="str">需清除格式的字符串</param>
/// <returns></returns>
string Stacking(string str)
{
MatchCollection mc = Regex.Matches(str, "\\\\S.*?;");
foreach (Match match in mc)
{
string mvla = Regex.Match(match.Value, "(?<=\\\\S).*?(?=;)").Value;
if (Regex.IsMatch(mvla, "\\^"))
{
if (Regex.IsMatch(mvla, "\\\\\\^"))
{
mvla = Regex.Replace(mvla, "\\\\\\^", @"\x01");
mvla = Regex.Replace(mvla, "\\^", "");
mvla = Regex.Replace(mvla, @"\\x01", "^");
}
else
{
mvla = Regex.Replace(mvla, "\\^", "");
}
str = str.Replace(match.Value, mvla);
}
else if (Regex.IsMatch(mvla, "\\#"))
{
if (Regex.IsMatch(mvla, "\\\\\\#"))
{
mvla = Regex.Replace(mvla, "\\\\\\#", @"\x01");
mvla = Regex.Replace(mvla, "\\#", "");
mvla = Regex.Replace(mvla, @"\\x01", "#");
}
else
{
mvla = Regex.Replace(mvla, "\\#", "");
}
str = str.Replace(match.Value, mvla);
}
else if (Regex.IsMatch(mvla, "\\/"))
{
if (Regex.IsMatch(mvla, "\\\\\\/"))
{
mvla = Regex.Replace(mvla, "\\\\\\/", @"\x01");
mvla = Regex.Replace(mvla, "\\/", "");
mvla = Regex.Replace(mvla, @"\\x01", "/");
}
else
{
mvla = Regex.Replace(mvla, "\\/", "");
}
str = str.Replace(match.Value, mvla);
}
}
if (Regex.IsMatch(str, @"(?<=(m|M))\\U\+00B3")) str = Regex.Replace(str, @"(?<=(m|M))\\U\+00B3", "3");
if (Regex.IsMatch(str, @"(?<=(m|M))\\U\+00B2")) str = Regex.Replace(str, @"(?<=(m|M))\\U\+00B2", "2");
if (Regex.IsMatch(str, @"%%(d|D)")) str = Regex.Replace(str, @"%%(d|D)", "°");
if (Regex.IsMatch(str, @"%%(c|C)")) str = Regex.Replace(str, @"%%(c|C)", "φ");
if (Regex.IsMatch(str, @"%%(p|P)")) str = Regex.Replace(str, @"%%(p|P)", "±");
return str;
}
}