一行java脚本中的两个正则表达式

时间:2017-10-31 11:14:58

标签: javascript regex

我在javascript中对一行进行了两次正则表达式检查

^(.*?(\b([A-Z]{2})([0-9]{7})\b)[^$]*)$

^(.*?(\b([A-Z]{3})([A-Z]{2})([0-9]{7})\b)[^$]*)$

问题是:

如果用户添加AAA AA1234567脚本检测第一个正则表达式而不是第二个。任何帮助如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

如果我正确理解了问题,则Option Explicit Sub test() 'declaration Dim ws1 As Worksheet, ws2 As Worksheet Dim c1 As Range, c2 As Range, rng1 As Range, rng2 As Range, cfind As Range 'set worksheets Set ws1 = ActiveWorkbook.Sheets(1) Set ws2 = ActiveWorkbook.Sheets(2) 'define ranges to look in/for With ws1 Set rng1 = .Range(.Cells(2, 2), .Cells(.Cells(Rows.Count, 2).End(xlUp).Row, 2)) End With With ws2 Set rng2 = .Range(.Cells(2, 2), .Cells(.Cells(Rows.Count, 2).End(xlUp).Row, 2)) End With 'loop through the values in sheet 1 For Each c1 In rng1 'loop through the values in sheets 2 For Each c2 In rng2 On Error Resume Next 'look for the value from sheet 2, in sheet 1 Set cfind = c1.Find(what:=c2.Value, lookat:=xlPart, LookIn:=xlValues) 'is a partial match found? then copy the value from column sheet2-colA from c2 to sheet1-colA for c1 If (Not (cfind Is Nothing)) Then c1.Offset(0, -1).Value = c2.Offset(0, -1).Value End If 'emtpy the found range Set cfind = Nothing Next c2 Next c1 'SUCCESS!! End Sub 文字应与两种模式相匹配。第一种模式已经奏效:

AAA AA1234567

第二个模式只需要两个标记之间的空格,如下所示:

^(.*?(\b([A-Z]{2})([0-9]{7})\b)[^$]*)$