Posts Tagged ‘正则表达式’

24
Mar

EmEditor 宏——行数统计+规约测试

   Posted by: 乐乐猪    in 软件开发

EmEditor 宏
功能:用来统计修改项目的代码行数,简单的规约测试。

修改过的部分在原程序的基础上追加以下代码:

1
2
3
4
5
6
7
8
9
Insert
//↓2008/03/10 Begin CompanyName UserName INS
//↑2008/03/10 End CompanyName UserName INS
Update
//↓2008/03/10 Begin CompanyName UserName UPD
//↑2008/03/10 End CompanyName UserName UPD
Delete
//↓2008/03/10 Begin CompanyName UserName DEL
//↑2008/03/10 End CompanyName UserName DEL

实际代码行数 = 追加的代码 + 删除的代码 + 修改后的代码(修改时被注释的部分不算)

文件名:EditCountFormat.vbee

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
'2008.02.29 修改代码行统计
'2008.02.29 修改代码行统计修正 UPD注释部分不算统计行
'2008.03.05 追加 修改代码的拷贝
'           追加 Tab存在Check
'           追加 结尾多余空格Check
'           追加 重复空格Check
'           追加 全角空格Check
'           追加 带*号的引用Check
'           追加 无用的换行Check
'           追加 左小括号Check
'           追加 逗号Check
Public GstrBegin, GstrEnd 
Public GintBegin, GintEnd, GintCount, GintOldBegin, GintOldEnd
GstrBegin = "^[ \t]*//↓.*Begin[ \t]XXXX.*"
GstrEnd = "^[ \t]*//↑.*End[ \t]XXXX.*"
 
document.selection.StartOfDocument false
 
blnEnd = False
GintCount = 0
strCountMsg = ""
strAllEditText = ""
 
Do While not blnEnd
    GintBegin = 0
    GintEnd = 0
    blnBeginOK = False
    blnEndOK = False
    intEditBegin = 0
    strSelectTxt = ""
    intEndx = 0
    if Document.selection.Find(GstrBegin, eeFindNext + eeFindReplaceRegExp) = 1 Then
      GintBegin = Document.selection.GetActivePointY(eePosView)
      strSelectTxt = Document.selection.Text
      intEditBegin = GintBegin
      GintOldBegin = GintBegin
      if Instr(strSelectTxt, "UPD") > 0 Then
        GintBegin = GintBegin + 1
        Do
          document.selection.SetActivePoint eePosView, 1, GintBegin
          document.selection.SelectLine
          strSelectTxt = Document.selection.Text
          if Instr(strSelectTxt, "//") = 0 Then
            GintBegin = GintBegin - 1
            Exit Do
          Else
            GintBegin = GintBegin + 1
          End If
        Loop
      End If
      blnBeginOK = True
    Else
      blnEnd = True
    End If
 
    if Document.selection.Find(GstrEnd, eeFindNext + eeFindReplaceRegExp) = 1 Then
      GintEnd = Document.selection.GetActivePointY(eePosView)
      intEndx = Document.selection.GetActivePointX(eePosView)
      GintOldEnd = GintEnd
      blnEndOK = True
    Else
      blnEnd = True
    End If
 
    if blnBeginOK and not blnEndOK Then
      alert "注释不匹配,有开始没有结束!" + chr(13) + chr(10) _
          + " 上次开始行 " + cstr(GintOldBegin) + " 上次结束行 " + cstr(GintOldEnd)
    Elseif not blnBeginOK and blnEndOK Then
      alert "注释不匹配,有结束没有开始! 结束行 " + cstr(GintEnd) + chr(13) + chr(10) _
          + " 上次开始行 " + cstr(GintOldBegin) + " 上次结束行 " + cstr(GintOldEnd)
    Elseif not blnBeginOK and not blnEndOK Then
      strCountMsg = "统计结束,修改的代码行数为 """ + cstr(GintCount) + " 行"""
    Else
      GintCount = GintCount + GintEnd - GintBegin - 1
 
      '2008.03.05 Add-------------------->
      Do While intEditBegin <> GintEnd + 1
        document.selection.SetActivePoint eePosView, 1, intEditBegin
        document.selection.SelectLine
        strAllEditText = strAllEditText + Document.selection.Text
        intEditBegin = intEditBegin + 1
      Loop
      document.selection.SetActivePoint eePosView, intEndx, GintEnd
      '2008.03.05 Add<--------------------
    End If
Loop
 
alert strCountMsg
 
'创建新文档
editor.NewFile
document.ConfigName = "Java"
'输出检查结果
document.write strAllEditText
 
'--------------------------------------------------------------------------------
Public pErrArray ()
 
Redim pErrArray(1)
 
'Tab存在Check
call checkRegExp("\t+.*$", "存在Tab键")
'结尾多余空格Check
call checkRegExp("^.*\s+$", "结尾多余空格")
'重复空格Check
call checkRegExp("(^\ +(?!\ ).*\ {2}.*$)|(^(?!\ )+.*\ {2}.*$)", "重复空格")
'全角空格Check
call checkRegExp(" ", "包含全角空格")
'带*号的引用Check
call checkRegExp("^import.*\*.*$", "带*号的引用")
'无用的换行Check
call checkRegExp("^[\s*]*\n[\s*]*$", "无用的换行")
'左小括号Check
call checkRegExp("^\s*if\(|^.*=\(|^.*\( ", "左括号空格不正")
'右小括号Check(未完成)
'call checkRegExp(, )
'逗号Check
call checkRegExp("^.*,(?! )\b", "逗号空格不正")
 
'---------------------------------- Check结束 ---------------------------------
'创建新文档
editor.NewFile
document.ConfigName = "Java"
strErrMessage = ""
strErrMessage = strErrMessage & strCountMsg & chr(13) & chr(10)
For intItem = 1 to UBound (pErrArray)
    strErrMessage = strErrMessage & pErrArray(intItem) & chr(13) & chr(10)
Next
'输出检查结果
document.write strErrMessage
 
'------------------------------------------------------------------------------
'正则表达式Check
Private Sub checkRegExp(strRegExp, errMsg)
    '将光标定位到文件头
    document.selection.StartOfDocument false
    Do While Document.selection.Find (strRegExp, eeFindNext + eeFindReplaceRegExp) = 1
        call inputError(errMsg)
    Loop
End Sub 
'------------------------------------------------------------------------------
'错误输入
Private Sub inputError(errType)
    '行定位
    errRow = Document.selection.GetActivePointY(eePosView)
    '错误消息
    strErrMsg = "Row: " & errRow & " Type: " & errType & " | "
    strErrMsg = strErrMsg & Document.GetLine(errRow)
    pErrArray(UBound(pErrArray)) = strErrMsg
    ReDim Preserve pErrArray (UBound (pErrArray) + 1) 
    '同类型错误排除
    document.selection.LineDown 
End Sub

中文关键字:emeditor macro regular expression 正则表达式 company update xp 统计 测试 selection document gintbegin 代码 gintend

Tags: , , , ,

24
Mar

常用正则表达式

   Posted by: 乐乐猪    in 软件开发

前一段时间写了2段EmEditor的宏,用来统计代码行数和简单的规约检查,稍微整理一下,
下面是从EmEditor的Q&A的提取的实例:

  • 双引号包含的字符串
    strings surrounded by double-quotation marks
    “.*?”
  • [ ]包含的字符串
    strings surrounded by [ ]
    \[[^\[]*?\]
  • 变量名
    variable names
    [a-zA-Z_][a-zA-Z_0-9]*
  • IP 地址
    IP addresses
    ([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})
  • 网页地址
    URL
    (\S+)://([^:/]+)(:(\d+))?(/[^#\s]*)(#(\S+))?
  • 各行Tab以后的文字列
    lines followed by a tab
    \t.*$
  • 平仮名 ひらがな
    Hiragana
    [\x{3041}-\x{309e}]
  • 全角片仮名 全角カタカナ
    Full-width Katakana
    [\x{309b}-\x{309c}\x{30a1}-\x{30fe}]
  • 半角仮名 半角カナ
    Half-width Kana
    [\x{ff61}-\x{ff9f}]
  • 中日韩 汉字
    CJK ideographs
    [\x{3400}-\x{9fff}\x{f900}-\x{fa2d}]
  • 中日韩 汉字符号
    CJK ideograph marks
    [\x{3000}-\x{3037}]
  • 韩国字符
    Hangul
    [\x{1100}-\x{11f9}\x{3131}-\x{318e}\x{ac00}-\x{d7a3}]
  • 行头插入 //
    Insert // at start of lines
    Find: ^
    Replace with: //
  • 删除行头 //
    Remove // at end of lines
    Find: ^//
    Replace:
  • 删除行后的空白文字(包含空格和制表位 Space+Tab)
    Remove trailing whitespaces
    Find: \s+?$
    Replace with:
  • 将(abc)替换为[abc]
    Replace (abc) with [abc]
    Find: \((.*?)\)
    Replace: \[\1\]
  • 将<H3 …>替换为<H4 …>
    Replace <H3 …> with <H4 …>
    Find: <H3(.*?)>
    Replace: <H4\1>
  • 将9/13/2003替换为2003年9月13日
    Replace 9/13/2003 with 2003.9.13
    Find: ([0-9]{1,2})/([0-9]{1,2})/([0-9]{2,4})
    Replace: \3年\1月\2日
  • 将字母a-z替换为大写字母
    Uppercase characters from a to z
    Find: [a-z]
    Replace: \U\0
  • 首字母大写
    Capitalize all words
    Find: ([a-zA-Z])([a-zA-Z]*)
    Replace: \U\1\L\2

  • 中文关键字:regular expression 正则表达式 emeditor 符号 正则 表达式 常用 replace 全角 find 汉字 半角 with 行头

    Tags: ,

    Page 1 of 11