我试图从以下示例中提取一些数据:
我希望我的结果分别是:
我很高兴使用表达式语法在多次传递中执行此操作,但我认为这不会真的有用。
我无法使用前瞻和后视来获取数据并排除“11-mill”和“XY-2822”之类的内容。我发现的事情是我能够排除那些匹配,但最终会为其他匹配截断好的结果。
最好的方法是什么?
我现在的正则表达式是
/(?:(\d+)[b\b\/-])([b\d\b]*)[^a-z]/i
捕获字母'b'(没关系)但在最后一个例子中没有捕获34b
答案 0 :(得分:2)
不确定您的具体要求/格式是什么,但您可以尝试:
/(?:\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|$)
其他版本here。
答案 1 :(得分:1)