gst 组正则表达式不匹配产生异常
gst regular expression mismatch of group generates exception
我在 GNU Smalltalk 3.2.5 中有一个尝试对键值设置进行分组匹配的简单示例:
st> m := 'a=b' =~ '(.*?)=(.*)'
MatchingRegexResults:'a=b'('a','b')
以上示例按预期工作。但是,如果没有匹配到第二组(.*)
,则会产生异常:
st> m := 'a=' =~ '(.*?)=(.*)'
Object: Interval new "<-0x4ce2bdf0>" error: Invalid index 1: index out of range
SystemExceptions.IndexOutOfRange(Exception)>>signal (ExcHandling.st:254)
SystemExceptions.IndexOutOfRange class>>signalOn:withIndex: (SysExcept.st:660)
Interval>>first (Interval.st:245)
Kernel.MatchingRegexResults>>at: (Regex.st:382)
Kernel.MatchingRegexResults>>printOn: (Regex.st:305)
Kernel.MatchingRegexResults(Object)>>printString (Object.st:534)
Kernel.MatchingRegexResults(Object)>>printNl (Object.st:571)
我不明白这种行为。我本以为结果是 ('a', nil)
而 m at: 2
是 nil
。我尝试了如下不同的方法:
st> 'a=' =~ '(.*?)=(.*)' ifMatched: [ :m | 'foo' printNl ]
'foo'
'foo'
这正确地确定了与正则表达式的匹配。但我仍然无法检查特定组是否为 nil
:
st> 'a=' =~ '(.*?)=(.*)' ifMatched: [ :m | (m at: 2) ifNotNil: [ (m at: 2) printNl ] ]
Object: Interval new "<-0x4ce81b58>" error: Invalid index 1: index out of range
SystemExceptions.IndexOutOfRange(Exception)>>signal (ExcHandling.st:254)
SystemExceptions.IndexOutOfRange class>>signalOn:withIndex: (SysExcept.st:660)
Interval>>first (Interval.st:245)
Kernel.MatchingRegexResults>>at: (Regex.st:382)
optimized [] in UndefinedObject>>executeStatements (a String:1)
Kernel.MatchingRegexResults>>ifNotMatched:ifMatched: (Regex.st:322)
Kernel.MatchingRegexResults(RegexResults)>>ifMatched: (Regex.st:188)
UndefinedObject>>executeStatements (a String:1)
nil
st>
我不明白这种行为。我本以为结果是 ('a', nil)
而 m at: 2
是 nil
。至少这就是它在我使用正则表达式的任何其他语言中的工作方式。这让我觉得也许我没有用我的语法做正确的事情。
我的问题是:我是否有正确的语法来尝试像这样匹配 ASCII 键值对(例如,在解析环境设置中)?如果我这样做了,为什么会生成异常,或者有什么方法可以让它提供一个我可以在不生成异常的情况下检查的结果?
我发现 related issue 从 2013 年 12 月起在 gnu.org 报告,但没有任何回复。
收到上述报告后,该问题已在 master 中修复。可以看到提交 here。 glib 事件循环集成目前阻止了稳定版本。
ValidationExpression="[0-9]{2}[(a-z)(A-Z)]{5}\d{4}[(a-z)(A-Z)]{1}\d{1}Z\d{1}"
SetFocusOnError="true" ControlToValidate="txtGST" Display="Dynamic" runat="server" ErrorMessage="Invalid GST No." ValidationGroup="Add" ForeColor="Red"></asp:RegularExpressionValidator>
我在 GNU Smalltalk 3.2.5 中有一个尝试对键值设置进行分组匹配的简单示例:
st> m := 'a=b' =~ '(.*?)=(.*)'
MatchingRegexResults:'a=b'('a','b')
以上示例按预期工作。但是,如果没有匹配到第二组(.*)
,则会产生异常:
st> m := 'a=' =~ '(.*?)=(.*)'
Object: Interval new "<-0x4ce2bdf0>" error: Invalid index 1: index out of range
SystemExceptions.IndexOutOfRange(Exception)>>signal (ExcHandling.st:254)
SystemExceptions.IndexOutOfRange class>>signalOn:withIndex: (SysExcept.st:660)
Interval>>first (Interval.st:245)
Kernel.MatchingRegexResults>>at: (Regex.st:382)
Kernel.MatchingRegexResults>>printOn: (Regex.st:305)
Kernel.MatchingRegexResults(Object)>>printString (Object.st:534)
Kernel.MatchingRegexResults(Object)>>printNl (Object.st:571)
我不明白这种行为。我本以为结果是 ('a', nil)
而 m at: 2
是 nil
。我尝试了如下不同的方法:
st> 'a=' =~ '(.*?)=(.*)' ifMatched: [ :m | 'foo' printNl ]
'foo'
'foo'
这正确地确定了与正则表达式的匹配。但我仍然无法检查特定组是否为 nil
:
st> 'a=' =~ '(.*?)=(.*)' ifMatched: [ :m | (m at: 2) ifNotNil: [ (m at: 2) printNl ] ]
Object: Interval new "<-0x4ce81b58>" error: Invalid index 1: index out of range
SystemExceptions.IndexOutOfRange(Exception)>>signal (ExcHandling.st:254)
SystemExceptions.IndexOutOfRange class>>signalOn:withIndex: (SysExcept.st:660)
Interval>>first (Interval.st:245)
Kernel.MatchingRegexResults>>at: (Regex.st:382)
optimized [] in UndefinedObject>>executeStatements (a String:1)
Kernel.MatchingRegexResults>>ifNotMatched:ifMatched: (Regex.st:322)
Kernel.MatchingRegexResults(RegexResults)>>ifMatched: (Regex.st:188)
UndefinedObject>>executeStatements (a String:1)
nil
st>
我不明白这种行为。我本以为结果是 ('a', nil)
而 m at: 2
是 nil
。至少这就是它在我使用正则表达式的任何其他语言中的工作方式。这让我觉得也许我没有用我的语法做正确的事情。
我的问题是:我是否有正确的语法来尝试像这样匹配 ASCII 键值对(例如,在解析环境设置中)?如果我这样做了,为什么会生成异常,或者有什么方法可以让它提供一个我可以在不生成异常的情况下检查的结果?
我发现 related issue 从 2013 年 12 月起在 gnu.org 报告,但没有任何回复。
收到上述报告后,该问题已在 master 中修复。可以看到提交 here。 glib 事件循环集成目前阻止了稳定版本。
ValidationExpression="[0-9]{2}[(a-z)(A-Z)]{5}\d{4}[(a-z)(A-Z)]{1}\d{1}Z\d{1}"
SetFocusOnError="true" ControlToValidate="txtGST" Display="Dynamic" runat="server" ErrorMessage="Invalid GST No." ValidationGroup="Add" ForeColor="Red"></asp:RegularExpressionValidator>