我有一个RegEx,它捕获了几个组:
VAN - H.Sedin (D.Sedin, A.Edler)
^(\w+)\s-\s(?P<goalscorer>.+)\s\((?P<assist1>.+),\s(?P<assist2>.+)\)$
goalscorer = H.Sedin
assist1 = D.Sedin
assist2 = A.Edler 我希望这样做,如果在行的末尾有一个空格,它仍然捕获它,因为有时在行的末尾可能会有一个空格。
我试着做了很多事情,最近:
^(\w+)\s-\s(?P<goalscorer>.+)\s\((?P<assist1>.+),\s(?P<assist2>.+)\)$|\)\s+$但我现在没办法抓住他们。
这里有一个实时链接来测试它:
发布于 2015-08-07 17:40:29
问题是你的regex
^(\w+)\s-\s(?P<goalscorer>.+)\s\((?P<assist1>.+),\s(?P<assist2>.+)\)$|\)\s+$
正在寻找两种选择之一:
^(\w+)\s-\s(?P<goalscorer>.+)\s\((?P<assist1>.+),\s(?P<assist2>.+)\)$或\)\s+$
但你想到的替代方案是\)$或\)\s+$
另一组限制备选方案的括号将解决这一问题:
^(\w+)\s-\s(?P<goalscorer>.+)\s\((?P<assist1>.+),\s(?P<assist2>.+)(\)$|\)\s+$)
或者,您可以直接使用\s*而不是\s+,还有一种替代方法,如:
^(\w+)\s-\s(?P<goalscorer>.+)\s\((?P<assist1>.+),\s(?P<assist2>.+)\)\s*$
发布于 2015-08-07 17:28:00
这是工作判据
^(?P<Name>\w+)\s-\s(?P<goalscorer>.+)\s\((?P<assist1>.+),\s(?P<assist2>.+)\)\s?$|\)注意\s?组后面的assist1。
发布于 2015-08-07 17:32:25
不清楚为什么使用|\)\s+$而不是用\s*$替换$。但这个方法很有效:
^(\w+)\s-\s(?P<goalscorer>.+)\s\((?P<assist1>.+),\s(?P<assist2>.+)\)\s*$https://stackoverflow.com/questions/31883349
复制相似问题