我试图从以下示例中提取一些数据:
我希望我的结果分别是:
我很乐意多次使用表达式语法来完成这个任务,尽管我不认为这会有什么帮助。
我很难用看头和后面的查找来获取数据,并排除诸如“11磨”和"XY-2822“之类的东西。我发现发生的是,我可以排除那些比赛,但最终截断好的结果,为其他比赛。
做这件事最好的方法是什么?
我现在的正则表达式是/(?:(\d+)[b\b\/-])([b\d\b]*)[^a-z]/i
它捕获字母'b‘(没关系),但在最后一个示例中没有捕获34b。
发布于 2015-09-23 21:30:05
不确定确切的要求/格式是什么,但您可以尝试如下:
/(?:\G(?!^)[-\/]|^(?:.*[^\d\/-])?)\K\d++(?![-\/]\D)/
http://rubular.com/r/WJqcCNe2pr
详情:
# two possible starts:
(?: # next occurrences
\G # anchor for the position after the previous match
(?!^) # not at the start of the line
[-\/]
| # first occurrence
^
(?:.*[^\d\/-])? # (note the greedy quantifier here,
# to obtain the last result of the line)
)
\K # discards characters matched before from the whole match
\d++ # several digits with a possessive quantifier to forbid backtracking
(?![-\/]\D) # not followed by an hyphen of a slash and a non-digit
如果将(?:.*[^\d\/-])?
替换为[^-\d\/\n]*+(?>[-\d\/]+[^-\d\/\n]+)*
,则可以改进模式(如果逐行工作,则删除\n
)。此更改的目标是限制回溯(每一个原子组发生原子组,而不是在第一个版本中逐字符进行)。
也许,您可以用这种正的展望:(?=[-\/]\d|b|$)
来代替负的展望
另一个版本的这里。
发布于 2015-09-23 21:18:38
https://stackoverflow.com/questions/32749230
复制相似问题