RegEx VBA Excel复杂的string

我有一个从这里拉的function。 我的问题是,我不知道我需要用什么RegEx模式来分割出以下数据:

+1 vorpal unholy longsword +31/+26/+21/+16 (2d6+13) +1 vorpal flaming whip +30/+25/+20 (1d4+7 plus 1d6 fire and entangle) 2 slams +31 (1d10+12) 

我希望它看起来像:

 +1 vorpal unholy longsword, 31 +1 vorpal flaming whip, 30 2 slams, 31 

这是进行RegExpvalidation的VBA代码:

 Public Function RXGET(ByRef find_pattern As Variant, _ ByRef within_text As Variant, _ Optional ByVal submatch As Long = 0, _ Optional ByVal start_num As Long = 0, _ Optional ByVal case_sensitive As Boolean = True) As Variant ' RXGET - Looks for a match for regular expression pattern find_pattern ' in the string within_text and returns it if found, error otherwise. ' Optional long submatch may be used to return the corresponding submatch ' if specified - otherwise the entire match is returned. ' Optional long start_num specifies the number of the character to start ' searching for in within_text. Default=0. ' Optional boolean case_sensitive makes the regex pattern case sensitive ' if true, insensitive otherwise. Default=true. Dim objRegex As VBScript_RegExp_55.RegExp Dim colMatch As VBScript_RegExp_55.MatchCollection Dim vbsMatch As VBScript_RegExp_55.Match Dim colSubMatch As VBScript_RegExp_55.SubMatches Dim sMatchString As String Set objRegex = New VBScript_RegExp_55.RegExp ' Initialise Regex object With objRegex .Global = False ' Default is case sensitive If case_sensitive Then .IgnoreCase = False Else: .IgnoreCase = True End If .pattern = find_pattern End With ' Return out of bounds error If start_num >= Len(within_text) Then RXGET = CVErr(xlErrNum) Exit Function End If sMatchString = Right$(within_text, Len(within_text) - start_num) ' Create Match collection Set colMatch = objRegex.Execute(sMatchString) If colMatch.Count = 0 Then ' No match RXGET = CVErr(xlErrNA) Else Set vbsMatch = colMatch(0) If submatch = 0 Then ' Return match value RXGET = vbsMatch.Value Else Set colSubMatch = vbsMatch.SubMatches ' Use the submatch collection If colSubMatch.Count < submatch Then RXGET = CVErr(xlErrNum) Else RXGET = CStr(colSubMatch(submatch - 1)) End If End If End If End Function 

我不知道有关Excel,但这应该让你开始在RegEx:

 /(?:^|, |and |or )(\+?\d?\s?[^\+]*?) (?:\+|-)(\d+)/ 

注:这里有一个小小的警告。 如果一个元素仅以+开始(不是后跟一个数字),这也会匹配。

捕获组1和2包含逗号左右的string(如果整个模式索引为0)。 所以你可以像capture[1] + ', ' + capture[2] (不pipe你的语法是什么)。

这里是对正则expression式的解释:

 /(?:^|, |and |or ) # make sure that we only start looking after # the beginning of the string, after a comma, after an # and or after an or; the "?:" makes sure that this # subpattern is not capturing (\+? # a literal "+" \d+ # at least one digit # a literal space [^+]*?) # arbitrarily many non-plus characters; the ? makes it # non-greedy, otherwise it might span multiple lines # a literal space \+ # a literal "+" (\d+)/ # at least one digit (and the brakets are for capturing)