我正在尝试开发一个regex表达式,它在连字符的第一个实例之前提取前几个字符,然后在第一个连字符之后保存第二组元素。
这里是大梁:
^([^-]*)(?(?=-)(\S.*)|())下面是几个测试用例:
SSB x Dj Chad - Crazy Beat - Tarraxo
Dj [R]afaa [F]ox -Tarraxo Do Inicio Das Aulas ( Nova Escola Producões )
Dj Snakes Share - MaloncyBeatz - Perfecto
Tarraxo Das Brasileiras [2014] [TxiGa Pro]IF语句完美地处理最后一个条件,但是对于前几个项,它返回第二个组'with‘连字符,而不是排除它。
换句话说:Dj Snakes Share - MaloncyBeatz - Perfecto应该返回:
Dj Snakes ShareMaloncyBeatz - Perfecto相反,第2组是:- MaloncyBeatz - Perfecto
更新
https://regex101.com/r/2BQPNg/12
使用^([^-]*)[^-]\W*(.*)可以工作,但对于最后一种情况(在没有连字符的情况下)会产生问题。它不包括]。
发布于 2017-08-24 20:30:51
我的解决方案:
^([^-]+?)\s*(?:-\s*(.*))?$
^ // start of line
([^-]+?) // 1+ not '-' chars, lazily matched (first captured group)
\s* // 0+ white-space chars
(?: // grouped, not captured
- // dash
\s*(.*) // 0+ white-space chars then anything (second captured group)
)? // 0 or 1 time
$ // end of line标志:全球,多行
501级减少到164级:
^[^-]+$|^((?:\w[^-]*)?\w)\W+(\w.*)
^ # start of line
[^-]+ # 1 or more not '-'
$ # end of line
| # OR
^ # start of line
( # start of group (captured)
(?: # start of group (not captured)
\w[^-]* # a word char then 0 or more not '-'
)? # 0 or 1 times
\w) # a word char, then end of group
\W+ # 1 or more non-word chars
(\w.*) # a word char then 0 or more anything (captured)发布于 2017-08-24 20:25:47
您正在使用这个正则表达式:
^([^-]*)[^-]\W*(.*)在这里,您的正则表达式中有一个额外的[^-],这将导致第一个组匹配比匹配少一个字符。
您可以使用这个regex:
^([^-]*)(?:\s+-\s*(.*))?$https://stackoverflow.com/questions/45870167
复制相似问题