明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 22026|回复: 35

[原创]获取MText中的可用字符

  [复制链接]
发表于 2004-5-20 00:31 | 显示全部楼层 |阅读模式
  1. Public Function LeftStr(ByVal String1 As Variant, ByVal String2 As Variant)
  2. On Error Resume Next
  3.     LeftStr = Left(String1, InStr(String1, String2) - 1)
  4.     If Err Then LeftStr = ""
  5. End Function
  6. Public Function RightStr(ByVal String1 As Variant, ByVal String2 As Variant)
  7. On Error Resume Next
  8.     RightStr = Right(String1, Len(String1) - Len(String2) - InStr(String1, String2) + 1)
  9.     If Err Then RightStr = ""
  10. End Function
  11. Public Function GetMTextString(str As String) As String
  12.     Dim pStrs As New Collection
  13.     Dim pStr As String, pType As String
  14.     Dim i, j
  15.     Dim pNum As Integer
  16.     pNum = InStr(str, "{")
  17.     Do While pNum > 0
  18.         If pNum = 1 Then
  19.             pStrs.Add RightStr(LeftStr(str, "}"), "{")
  20.             str = RightStr(str, "}")
  21.         Else
  22.             pStrs.Add LeftStr(str, "{")
  23.             str = "{" & RightStr(str, "{")
  24.         End If
  25.         pNum = InStr(str, "{")
  26.     Loop
  27.     If Trim(str) <> "" Then pStrs.Add str
  28.     For Each i In pStrs
  29.         For Each j In Split(i, "")
  30.             If InStr(i, "") > 0 Then
  31.                 pType = Left(j, 1)
  32.                 If pType = "f" Or pType = "F" Or pType = "c" Or pType = "C" Then
  33.                     pStr = pStr & RightStr(j, ";")
  34.                 Else
  35.                     pStr = pStr & RightStr(j, Mid(j, 1, 1))
  36.                 End If
  37.             Else
  38.                 pStr = pStr & i
  39.             End If
  40.         Next j
  41.     Next i
  42.     GetMTextString = pStr
  43. End Function

发表于 2018-6-6 18:34 | 显示全部楼层
非常精彩,可惜没有lisp版本的
发表于 2019-2-1 06:06 来自手机 | 显示全部楼层
我也正在找lisp版
发表于 2004-5-20 07:50 | 显示全部楼层
不用那么麻烦,炸开取单行文件的字符串的就可以了。
 楼主| 发表于 2004-5-20 08:26 | 显示全部楼层
是麻烦点,不过总觉得炸开取字符串不保险,如果用户来个取消,不知道到哪一步了


不过还没有考虑\s、\h、\u、\b这几种情况
 楼主| 发表于 2004-5-20 20:55 | 显示全部楼层
本帖最后由 作者 于 2004-6-25 23:29:37 编辑

这是改进版,已基本解决问题,欢迎测试
  1. Public Function FindStr(ByVal str As Variant, ByVal Target As Variant)
  2.        str = Replace(str, "\", "**")
  3.        str = Replace(str, "" & Target, "**")
  4.        FindStr = InStr(str, Target)
  5. End FunctionPublic Function LeftStr(ByVal String1 As Variant, ByVal String2 As Variant)
  6. On Error Resume Next
  7.        LeftStr = Left(String1, FindStr(String1, String2) - 1)
  8.        If Err Then LeftStr = ""
  9. End FunctionPublic Function RightStr(ByVal String1 As Variant, ByVal String2 As Variant)
  10. On Error Resume Next
  11.        RightStr = Right(String1, Len(String1) - Len(String2) - FindStr(String1, String2) + 1)
  12.        If Err Then RightStr = ""
  13. End FunctionPublic Function FindMirrorString(ByVal str As String, ByVal StartStr As String, ByVal EndStr As String) As Integer
  14.        Dim pStart As Integer, pEnd As Integer
  15.        Dim pSNum As Integer, pENum As Integer
  16.        FindMirrorString = FindStr(str, StartStr)
  17.        If FindMirrorString = 0 Then Exit Function
  18.        FindMirrorString = 0
  19.        Do While pStart = 0 Or pEnd = 0 Or pStart <> pEnd
  20.                pSNum = FindStr(str, StartStr): pENum = FindStr(str, EndStr)
  21.                If pENum = 0 Then
  22.                        FindMirrorString = 0: Exit Function
  23.                ElseIf pSNum < pENum And pSNum <> 0 Then
  24.                        pStart = pStart + 1
  25.                        FindMirrorString = FindMirrorString + pSNum
  26.                        str = RightStr(str, StartStr)
  27.                Else
  28.                        pEnd = pEnd + 1
  29.                        FindMirrorString = FindMirrorString + pENum
  30.                        str = RightStr(str, EndStr)
  31.                End If
  32.        Loop
  33. End FunctionPublic Function SplitMTextString(ByVal str As String, ByVal StartStr As String, ByVal EndStr As String) As Collection
  34. On Error Resume Next
  35.        Dim pStr As String
  36.        Dim pStrs As New Collection
  37.        Dim pStart As Integer
  38.        Dim pEnd As Integer
  39.        Dim pNum As Integer
  40.        pNum = FindStr(str, StartStr)
  41.        If pNum = 0 Then
  42.                GoTo EndHandle
  43.        ElseIf pNum > 1 Then
  44.                pStrs.Add LeftStr(str, StartStr)
  45.                str = "{" & RightStr(str, StartStr)
  46.        End If
  47.        pNum = 1
  48.        Do While pNum > 0
  49.                pStart = pNum
  50.                pEnd = FindMirrorString(str, StartStr, EndStr)
  51.                If FindStr(str, StartStr) > 0 Then pStrs.Add LeftStr(str, StartStr)
  52.                pStr = Mid(str, pStart + 1, pEnd - pStart - 1)
  53.                        For Each i In SplitMTextString(pStr, StartStr, EndStr)
  54.                                pStrs.Add i
  55.                        Next i
  56.                Err.Clear
  57.                str = Right(str, Len(str) - pEnd)
  58.                pNum = FindStr(str, StartStr)
  59.        Loop
  60. EndHandle:
  61.        pStrs.Add str
  62.        Set SplitMTextString = pStrs
  63. End FunctionPublic Function SplitRtfString(ByVal str As String) As Collection
  64.        Dim pSplit As Variant
  65.        Dim pStr As String
  66.        Dim pStrs As New Collection
  67.        Dim i As Integer
  68.        If str = "" Then GoTo EndHandle
  69.        str = "L" & str
  70.        pSplit = Split(str, "")
  71.        i = 0
  72.        Do While i <= UBound(pSplit)
  73.        If pSplit(i) = "" Then
  74.                If pStrs.Count > 0 Then
  75.                        pStr = pStrs(pStrs.Count)
  76.                        pStrs.Remove pStrs.Count
  77.                End If
  78.                pStrs.Add pStr & "" & pSplit(i + 1)
  79.                i = i + 1
  80.        ElseIf Asc(pSplit(i)) = 123 Or Asc(pSplit(i)) = 125 Then
  81.                If pStrs.Count > 0 Then
  82.                        pStr = pStrs(pStrs.Count)
  83.                        pStrs.Remove pStrs.Count
  84.                End If
  85.                pStrs.Add pStr & pSplit(i)
  86.        Else
  87.                pStrs.Add pSplit(i)
  88.        End If
  89.        i = i + 1
  90.        Loop
  91. EndHandle:
  92.        Set SplitRtfString = pStrs
  93. End Function
  94. Public Function GetMTextString(str As String) As String
  95.        Dim pStr As String, pType As Long
  96.        Dim i, j
  97.       
  98.        For Each i In SplitMTextString(str, "{", "}")
  99.                For Each j In SplitRtfString(i)
  100.                        If CStr(UCase(Left(j, 1))) <> "" Then
  101.                                pType = Asc(CStr(UCase(Left(j, 1))))
  102.                                Select Case pType
  103.                                Case 65, 67, 70, 72, 81, 84, 87                                         'A/C/F/H/Q/T/W
  104.                                        pStr = pStr & RightStr(j, ";")
  105.                                Case 76, 79, 80                                                                         'L/O/P
  106.                                        pStr = pStr & RightStr(j, Left(j, 1))
  107.                                Case 83                                                                                         'S
  108.                                        j = Mid(j, 2, Len(j) - 2)
  109.                                        pStr = pStr & Replace(j, "^", "")
  110.                                Case 85                                                                                         'U
  111.                                        pStr = pStr & "" & j
  112.                                Case 126                                                                                       '~
  113.                                        pStr = pStr & Replace(j, "~", " ")
  114.                                Case Else
  115.                                        pStr = pStr & j
  116.                                End Select
  117.                        End If
  118.                Next j
  119.        Next i
  120.       
  121.        GetMTextString = pStr
  122. End Function

评分

参与人数 1威望 +1 金钱 +10 贡献 +5 激情 +5 收起 理由
mccad + 1 + 10 + 5 + 5 【精华】好程序

查看全部评分

发表于 2004-6-23 18:09 | 显示全部楼层
什么叫 MText中的可用字符 ???
发表于 2004-6-23 21:34 | 显示全部楼层
引用以下控件后,就可以用以下程序来做。
详细就留给飞狐版主完善了。应该说使用这个控件使程序更加简单  
  1. Sub MtToDt()
  2.        Dim s As String
  3.        Dim RE As RegExp
  4.        Set RE = New RegExp
  5.        RE.IgnoreCase = True
  6.        RE.Global = True
  7.        s = "\A1;明{\H2.4x;经通}道{\fTimes|b0|i0|c0|p18;http://www.mjtd.com}是\P一个{\H0.6978x;\S好^网站;}啊,{\L呵呵}"
  8.        '处理文字大小
  9.        RE.Pattern = "\{\\H(.[^;}]*);(.[^}]*)\}"
  10.        s = RE.Replace(s, "$2")
  11.        '处理堆迭
  12.        RE.Pattern = "\\S(.[^;]*)\^(.[^;]*);"
  13.        s = RE.Replace(s, "$1$2")
  14.        '处理字体
  15.        RE.Pattern = "\{\\f(.[^}]*);(.[^}]*)\}"
  16.        s = RE.Replace(s, "$2")
  17.        '处理文字首位置字符
  18.        RE.Pattern = "\\A(.[^}]*);"
  19.        s = RE.Replace(s, "")
  20.        '处理下划线
  21.        RE.Pattern = "\{\\L(.[^;}]*)\}"
  22.        s = RE.Replace(s, "$1")
  23.        '处理换行符
  24.        RE.Pattern = "\\P"
  25.        s = RE.Replace(s, "")
  26.        Debug.Print s
  27.        Set RE = Nothing
  28.       
  29. End Sub
处理完后就剩下这一部分了:
  1. 明经通道http://www.mjtd.com是一个好网站啊,呵呵
复制代码

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

x
 楼主| 发表于 2004-6-23 21:40 | 显示全部楼层
可是RTF最多有8层嵌套RegExp是怎么处理的呢
发表于 2004-6-23 21:45 | 显示全部楼层
确实得详细考虑嵌套关系
 楼主| 发表于 2004-6-23 21:53 | 显示全部楼层
本帖最后由 作者 于 2004-6-23 23:03:12 编辑

可能还是要找到镜像字符对,好像和我的程序差不多了?


或者用堆栈直接处理应该简单一点吧
发表于 2004-6-24 21:18 | 显示全部楼层
lzh741206能否提供一个复杂的MTEXT格式的样例用于调试程序用。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-5-19 05:51 , Processed in 0.213223 second(s), 27 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表